JUnit and junit.framework.TestSuite - No runnable methods

I've made some unit tests (in test class). The tutorial I've read said that I should make a TestSuite for the unittests.

Odd is that when I'm running the unit test directly (selecting the test class - Run as jUnit test) everything is working fine, altough when I'm trying the same thing with the test suite there's always an exception: java.lang.Exception: No runnable methods.

Here is the code of the test suite:

import junit.framework.Test;
import junit.framework.TestSuite;

public class AllTests {

public static Test suite() {
    TestSuite suite = new TestSuite("Test suite for com.xxx.yyyy.test");
    //$JUnit-BEGIN$
    suite.addTestSuite(TestCase.class);
    //$JUnit-END$
    return suite;
    }

}

Any ideas why this isn't working ?


Asked by: Elise864 | Posted: 21-01-2022






Answer 1

I'm not experienced in ant - so I'm not using it for testing it right now.

Searching the internet it seems like I'm mixing up the old jUnit 3.8 and jUnit 4.0 behavior. Trying now a way to use the "new behavior"

edited:
now it works:

AllTest changed to:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;


@RunWith(value=Suite.class)
@SuiteClasses(value={TestCase.class})
public class AllTests {

}

TestCase changed to:

import static org.junit.Assert.assertTrue;
import org.junit.Test;

public class TestCase  {
@Test
    public void test1 {
        assertTrue (tmp.getTermin().equals(soll));
    }
}

Answered by: Julia161 | Posted: 22-02-2022



Answer 2

Be careful when using an IDE's code-completion to add the import for @Test. It has to be import org.junit.Test and not import org.testng.annotations.Test, for example. If you use the second one by mistake, you'll get the "no runnable methods" error. (I was using Intellij Idea 2017 which imported org.junit.jupiter.api.Test instead!)

Answered by: Thomas650 | Posted: 22-02-2022



Answer 3

Took me a bit too to figure it out, but I think this solves your problem:

You're doing a suite.addTestSuite(TestCase.class), while you should've done a suite.addTest(TestCase.class).

You can also add a testsuite to a testsuite to create a whole hierarchy of testsuites. In that case you'll have to use suite.addTest(). But note that you then use .suite() and not .class: suite.addTest(MyTestSuite.suite())!

Answered by: Arthur764 | Posted: 22-02-2022



Answer 4

For sure, it won't work since you're not telling the test suite what are your test classes.

But I'm wondering why you're not using the "classical way" for building Test suites, which is ant using jUnit's ant tasks.

Answered by: Carina696 | Posted: 22-02-2022



Similar questions

java - The type junit.framework.TestCase cannot be resolved. It is indirectly referenced from required .class files

I'm following the example here: jMock - getting started I'm getting this error: The type junit.framework.TestCase cannot be resolved. It is indirectly referenced from required .class files I've imported these 4 jars: jmock-2.5.1.jar hamcrest-core-1.1.jar hamcrest-library-1.1.jar jmock-junit3-2....


Working on a legacy java program using NetBeans, I am unable to import the junit.framework.TestCase the original author used in Test.java files

To start things off, I am entirely new to Java. I'm a C#/Powershell guy. A client at my IT Firm had an issue with a java program that they were executing on a daily basis that was having issues. According to Windows, the original program was written in April of 2011. I was able to unzip the file and pulled out all of the java files. I then rebuilt the program's structure in NetBeans and am getting ready to start editing. H...






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top