How to add a JUnit 4 test that doesn't extend from TestCase to a TestSuite?

In JUnit 3 I simply called

suite.addTestSuite( MyTest.class )

However if MyTest is a JUnit 4 test which does not extend TestCase this doesn't work. What should I do instead to create a suite of tests?


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






Answer 1

For those with a large set of 3.8 style suites/tests that need to coexist with the new v4 style you can do the following:

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

@RunWith(Suite.class)
@Suite.SuiteClasses({
  // Add a JUnit 3 suite
  CalculatorSuite.class,
  // JUnit 4 style tests
  TestCalculatorAddition.class,
  TestCalculatorDivision.class
})
public class CalculatorSuite {
    // A traditional JUnit 3 suite
    public static Test suite() {
        TestSuite suite = new TestSuite();
        suite.addTestSuite(TestCalculatorSubtraction.class);
        return suite;
    }
}

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



Answer 2

Found the answer myself: here

Like so:

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

@RunWith(Suite.class)
@Suite.SuiteClasses({
  TestCalculatorAddition.class,
  TestCalculatorSubtraction.class,
  TestCalculatorMultiplication.class,
  TestCalculatorDivision.class
})
public class CalculatorSuite {
    // the class remains completely empty, 
    // being used only as a holder for the above annotations
}

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



Similar questions

java - Passing object from TestSuite to a TestCase in juint4

I want to pass an object (not just a string or integer) from a TestSuite to all the TestCases. In previous junit versions I could have pass an argument to the constructor before calling testSuite.addTest(new TestCase(someObject)) How can I do it now using the annotation style? Thanks


java - TestSuite Setup in jUnit 4

I've managed to find out how to make a TestSuite in jUnit 4, but I really miss the v3 possibility of wrapping a suite in a TestSetup. Any ideas as to how to get some @BeforeClass/@AfterClass setup executed for a suite of test cases in jUnit 4? I.e. @RunWith(Suite.class) @Suite.SuiteClasses({Test1.class, Test2.class}) public class MyTestSuite { @BeforeClass public static void setUpClass()...


java - JUnit4 run all tests in a specific package using a testsuite

Is this possible in JUnit4? In JUnit3, I would do the following: public class MyTestSuite { public static Test suite() throws Exception { doBeforeActions(); try { TestSuite testSuite = new TestSuite(); for(Class clazz : getAllClassesInPackage("com.mypackage")){ testSuite.addTestSuite(clazz); } return testSuite; } finally { doAfte...


java - Passing object from TestSuite to a TestCase in juint4

I want to pass an object (not just a string or integer) from a TestSuite to all the TestCases. In previous junit versions I could have pass an argument to the constructor before calling testSuite.addTest(new TestCase(someObject)) How can I do it now using the annotation style? Thanks


java - Adding Test to TestSuite in JUnit4

Is there a way to programmatically add test to a testsuite in JUnit4? In Junit3 you can do this TestSuite ts = new TestSuite(); ts.addTestSuite(a.class); ts.addTestSuite(b.class); How bout in JUnit4?


java - Can we write custom testsuite in JUNIT 4?

I am new to JUNIT 4, I want to write code such as test suite get inputs from excelsheet and based on input testsuite invoke diffrent test cases. As per JUNIT 4 we can define all test cases in @SuiteClasses({ ParameterSampleTest.class,SampleJUnitTest.class }) but this is when we have predefine test cases. but here i want to use custome JUNIT runner that takes decision based on input? can some one help to resolv...


java - Should JUnit3 TestSuite really be used like this?

I couldn't recall how to use JUnit3 TestSuite and after some Googling found this public class MyTestsuite extends TestSuite { public static Test suite() { final TestSuite s = new TestSuite(); s.addTestSuite(Test1.class); s.addTestSuite(Test2.class); return s; } } It seems to work, but looks very strange to me: Why should I ...


java - No runnable methods found Running multiple JUnit4 testSuite

I have the following testSuite package com.swaserver.junit; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; import junit.framework.TestSuite; public class MyTestSuite extends TestSuite { @RunWith(Suite.class) @SuiteClasses( { BinarySearchTest.class }) public class RunTestSuite { } } However, It tells me ...


java - Creating a pointcut in Aspectj for each Junit test in a testsuite

Closed. This question is not reproducible or was caused...


java - Passing Paramter to test method and running only specific method in testsuite

Here is my test suite @RunWith(Suite.class) @SuiteClasses({ LoginTest.class }) public class SmokeTestSuite { } Here is my LoginTest.class file public class LoginTest extends MSTestBase { @Test public void User1LoginTest() throws Exception { /*code*/ } @Test public void User2LoginTest() throws Exception { /*code*/ } @...


java - Running TestSuite in JUnit4 and Maven

I am integrating an external junit.framework.TestSuite into my library which is built using Maven and JUnit4. The typical tests, if not all of them, are kicked off using JUnit4 annotations. How do I incorporate a junit.framework.TestSuite into this existing test codebase? Here's what I have tried so far: public class JSR330Tck { @Test public junit.framework.Test s...






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