1

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);
    }
}
3
  • How are you trying to run your tests? From an IDE (which?) or a command line (which command?) or something else? Commented Apr 21 at 22:18
  • 4
    Please note that this question has come through the Staging Ground, with the intention that the OP answers it themselves. Their problem was solved by making the test method public. Commented Apr 21 at 22:29
  • Can you answer few more questions? 1. Which IDE are you using to run this test? 2. Have you added test jars in maven / gradle? 3. Are you seeing any compile time errors? I suppose no, as you are able to run test with no errors. 4. As @ekhumoro confirmed, can you try to remove private from method 'testThatWontExecute()' and try to run the test? Commented Apr 22 at 12:02

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.