Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using JUnit's @Parameterized, can I have some tests still run only once [duplicate]

I use @Parameterized in many cases to run tests on a number of permutations. This works very well and keeps the test-code itself simple and clean.

However sometimes I would like to have some of the test-methods still run only once as they do not make use of the parameters, is there a way with JUnit to mark the test-method as "singleton" or "run-once"?

Note: This does not concern running single tests in Eclipse, I know how to do that :)

like image 783
centic Avatar asked Sep 25 '15 06:09

centic


People also ask

Which of the tests enable the developer to run the same tests repeatedly?

JUnit 4 has introduced a new feature called parameterized tests. Parameterized tests allow a developer to run the same test over and over again using different values.

How do you repeat a JUnit test?

JUnit Jupiter @RepeatedTest annotation is used to repeat the test case for specified no. of times. Each invocation of the test case behaves like a regular @Test method, so it has support for the same lifecycle callbacks and extensions in JUnit 5.


2 Answers

You could structure your test with the Enclosed runner.

@RunWith(Enclosed.class) public class TestClass {      @RunWith(Parameterized.class)     public static class TheParameterizedPart {          @Parameters         public static Object[][] data() {             ...         }          @Test         public void someTest() {             ...         }          @Test         public void anotherTest() {             ...         }     }      public static class NotParameterizedPart {         @Test         public void someTest() {             ...         }     } } 
like image 137
Stefan Birkner Avatar answered Sep 24 '22 00:09

Stefan Birkner


You can associate any number of test classes to run together using a suite. This way all the tests are run when you test your class and you can mix different test runners.

  1. Create a test suite associated with the class you are testing
  2. Add a reference to the parameterized test class
  3. Add the other class(es) containing non parameterized tests.

    import org.junit.runners.Suite; import org.junit.runner.RunWith;  @RunWith(Suite.class) @Suite.SuiteClasses({ParameterizedTestClass.class, UnitTests.class, MoreUnitTests.class}) public class SutTestSuite{      //Empty... } 
like image 37
daver Avatar answered Sep 21 '22 00:09

daver