Create JUnit TestSuite non-statically

I am looking for a way to create and run JUnit TestSuite non-statically.

I am currently doing something like this:

public class MyTestSuite { public static TestSuite suite() { TestSuite suite = new TestSuite(); suite.addTest(...); suite.addTest(...); // .... return suite; } } 

I do this because I create TestCases, which I add to the package programmatically. With this solution, I ran into the problem that my MyTestSuite class is never created. I would like to associate it with a spring container, for example. using

 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={...}) @Transactional 

but I see no way to tell SpringJUnit4ClassRunner that it should also run my program tests.

Thank you for your help! Erik

+4
source share
3 answers

Why use a kit? It seems easier to put your tests in your own subdirectory and use ant (or any other build tool you use) that runs only those tests that are there.

+2
source

You can try MyTestSuite as part of your spring context (test context) and run the init method on it, which will add your program tests. This will allow you to enter MyTestSuite, which contains these software tests when it is instantiated by spring.

Hope this helps.

+1
source

For JUnit3-style suite methods, JUnit does not instantiate the class; it calls the method and calls run(TestResult) for the returned object.

SpringJUnit4ClassRunner is the JUnit4 Runner class, so it cannot be used to influence the behavior of test suites in the JUnit3 style. Spring does not provide a JUnit4-style package implementation. If you want each test case to use SpringJUnit4ClassRunner , your best option is to upgrade them to JUnit4.

If you ask how to add Spring tests to MyTestSuite :

 public class MyTestSuite { public static Test suite() { TestSuite suite = new TestSuite(); suite.addTest(...); suite.addTest(...); suite.addTest(new JUnit4TestAdapter(ExampleSpringTest.class)); // .... return suite; } } 
+1
source

Source: https://habr.com/ru/post/1342781/


All Articles