program tip

junit : 테스트를 찾을 수 없음

radiobox 2021. 1. 5. 07:55
반응형

junit : 테스트를 찾을 수 없음


저는 Java 프로젝트를 상속 받았으며 Java 개발이 처음입니다. 코드에 익숙해지는 좋은 방법은 주변에 테스트를 작성하는 것입니다. IntelliJ를 사용하여 코드를 작성하고 있습니다.

내 기존 프로젝트에는 다음과 같은 폴더 구조가 있습니다.

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

이 프로젝트에 대한 테스트를 보관할 새 프로젝트를 만들었습니다. 이 프로젝트에 단위 테스트와 통합 테스트가 모두 포함되기를 바랍니다. 현재 내 새 프로젝트는 다음과 같은 구조를 가지고 있습니다.

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

emailProviderTest.java 파일은 다음과 같습니다.

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);
    }
}

이 프로젝트에는 다음 속성이있는 실행 / 디버그 구성이 있습니다.

  • 테스트 종류 : 모든 패키지
  • 테스트 검색 : 전체 프로젝트에서

이 구성을 실행하면 다음과 같은 오류가 발생합니다.

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 error too

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

But I've forgot to specify

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

After sync projects it found 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'?


I was getting this too:

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

Solved by renaming the test method from method1

@Test
public void method1(){
    // Do some stuff...
}

to testMethod1

@Test
public void testMethod1(){
    // Do some stuff...
}

if you are using jUnit 4 then not use extends TestCase, removing this fixed error.


I had the same issue, in Intellij IDEA, for Java framework. I was doing all right:

  • the class inherited from TestCase
  • I've imported junit.framework.TestCase
  • I've add the decoration @Test

But I did one thing wrong: the name of the method didn't start with "test", in fact was:

@Test
public void getSomethingTest(){

and when I changed it into:

@Test
public void testGetSomethingTest(){

I've resolved: the executor was finally able to recognize this method as a test method. I've changed nothing else.


I had it when using data provider and one of the parameters had a new line character, like this:

@DataProvider
public static Object[][] invalidAdjustment() {
    return new Object[][]{
            {"some \n text", false},
            };
}

Removing the \n solved the issue


In my case, I needed junit4-version.jar in the classpath of the task junit and also:

ant-version.jar
ant-junit-version.jar
ant-junit4-version.jar

at the library of my ant installation (/usr/share/ant/lib).

I was getting the error "junit.framework.AssertionFailedError: No tests found in ..." while I hadn't had ant-junit4-*version*.jar in the right place.

I corrected this by installing ant-optional debian/ubuntu package:

apt-get install ant-optional

I was fretting with this and none of the answers helped me until i moved the test source file from this folder:

src/test/java/com/junit/test

up to this folder:

src/test/java/com/junit

The other reason generally i see people having method parameters, remove any parameters you might have in the test method, regardless you add @Test annotation , you need to have your method name starting "test" in order for Junit to pick you testcase. hope it helps.


Test method name public void testName() {}

Where name() in source naming name methods.

ReferenceURL : https://stackoverflow.com/questions/22469480/junit-no-tests-found

반응형