junit: no tests found

I have inherited a Java project and am new to Java development. I feel a good way for me to get comfortable with the code is to write some tests around it. I'm writing my code using IntelliJ.

My existing project has a folder structure like this:

/myProject
  /src
    /main
      /java
        /com.lexcorp
          /core
            /email
              /providers
                emailProvider.java

I created a new project that will hold tests for this project. I would like this project to hold both unit and integration tests. Currently, my new project has a structure like this:

/myProjectTests
  /src
    /main
      /java
        /com.lexcorp.core.email.providers
          emailProviderTest.java

The emailProviderTest.java file looks like the following:

package com.lexcorp.core.email.providers;

import junit.framework.TestCase;
import org.junit.Test;

public class EmailProviderTest extends TestCase {

    private final String username = "[testAccount]";

    private final String password = "[testPassword]";

    @Test
    public void thisAlwaysPasses() {
        assertTrue(true);
    }
}

This project has a Run/Debug configuration with the following properties:

  • Test kind: All in package
  • Search for tests: In whole project

When I run this configuration, I get an error that says:

junit.framework.AssertionFailedError: No tests found in com.lexcorp.core.email.providers.EmailProviderTest
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
    at org.junit.runners.Suite.runChild(Suite.java:127)
    at org.junit.runners.Suite.runChild(Suite.java:26)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

I do not understand why I'm getting an error that boils down to: "No tests found". While my project structures differ, the folder structures on the OS match (which is another thing that confuses me). Why am I getting this error and how do I fix it?


I was getting this error, too:

junit.framework.AssertionFailedError: No tests found in ...

The reason was that I forgot to specify

defaultConfig {
    ...
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

After syncing the project, it found the tests. So maybe it helps someone else.


Extending junit.framework.TestCase is the old JUnit 3 approach of implementing test cases which doesnt work as no methods start with the letters test. Since you're using JUnit 4, just declare the class as

public class EmailProviderTest {

and the test method will be found from the @Test annotation.

Read: JUnit confusion: use 'extend Testcase' or '@Test'?