6

Q&A-Style question as the existing questions don't match the simple typo I made here:

Goal

  • Execute simple JUnit tests via the IntelliJ IDE using the UI (right-click: run test)

Problem

  • IntelliJ tells that "no tests were found"

Code

import org.junit.jupiter.api.Test;

public class Test {
    @Test
    private void testAMethod() { (...) }
}

hb0
  • 3,350
  • 3
  • 30
  • 48

5 Answers5

16

change to

public void testAMethod() { (...) }

According to Junit5 document

Test classes, test methods, and lifecycle methods are not required to be public, but they must not be private.

https://junit.org/junit5/docs/current/user-guide/

Vikki
  • 1,897
  • 1
  • 17
  • 24
  • 2
    HOLLY #$#@$#@ I wish I could upvote this more times thanks – Leonardo Pina Sep 16 '20 at 01:51
  • Also, make sure that you did not accidentally mark the test method as `static` -- this leads to a similar "no tests were found" message – Martin J.H. Jul 05 '21 at 09:38
  • Makes no difference on IntelliJ IDEA 2021.3.2. The IDE fails to find any @Test annotated methods which follow the JUnit documentation exactly. – Dave Feb 24 '22 at 04:03
3

Another possibility of this error message is when using a non-void return type. Switch to void and it will work.

1

I know it is not relevant but if someone else is facing issue like me and none of the above solution works for you then check your target folder does it have classes files in this. I was having this same issue then I checked my target/classes folder and found it empty. Then I build my mvn again using mvn clean install and then it work like charm. when build was success but class file was not generated as I was using DskipTests

0

The issue was a simply typo - the test was not visible from outside:

@Test
// Before
private void testAMethod() { (...) 
// After
void testAMethod() { (...) 
hb0
  • 3,350
  • 3
  • 30
  • 48
-6

Junit in IntelliJ won't work on Java 10 or above. Try downgrading the project SDK to Java 8 and the tests will work like a charm.

Farrukh Chishti
  • 7,652
  • 10
  • 36
  • 60