My JUnit tests simply aren't running. When I tried, nothing happened. JUnit displayed 0/0 tests run, and there were no logs nor errors in the console
I checked the different possible causes in this post: JUnit Tests not running in Eclipse, but none of the answers worked for me.
I also tried commenting parts of my code until I was left with the most basic test possible with an assertTrue(false)
, but it still didn't run.
Here's a minimal example of a test that doesn't run:
import static org.junit.Assert.assertTrue;
import org.junit.jupiter.api.Test;
public class MinimalTest {
@Test
private void testThatWontExecute()
{
assertTrue(false);
}
}
So it turned out the problem was that I made the test method private. Once I removed the private and left the default (which is package-private, from what I understand), the test ran smoothly:
import static org.junit.Assert.assertTrue;
import org.junit.jupiter.api.Test;
class MinimalTest {
@Test
void testThatWillExecute() //No access modifier specified
{
assertTrue(false);
}
}
Now that I know what to look for, I've found that this is actually mentioned in the JUnit documentation at https://junit.org/junit5/docs/current/user-guide/#writing-tests-classes-and-methods, though I wonder why that is the case:
Class and method visibility
Test classes, test methods, and lifecycle methods are not required to be public, but they must not be private.
It is generally recommended to omit the public modifier for test classes, test methods, and lifecycle methods unless there is a technical reason for doing so – for example, when a test class is extended by a test class in another package. Another technical reason for making classes and methods public is to simplify testing on the module path when using the Java Module System.