Is there any annotation in JUnit to exclude a non param test in parameterized test class?
I stuck in this problem while I'm writing test in spring boot MockMvc I simply created two classes in separate java files ( one for ParameterizedTest and other for SingleTest ) and create a suite for them. because inner classes were creating error for static members and and not static members and class.
As of Junit 5.0.0 you can now annotate your test methods with @ParameterizedTest
. So no need for inner classes. There are many ways to supply the arguments to the parameterized test apart from ValueSource as shown below. See the official junit user guide for details:
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
public class ComponentTest {
@ParameterizedTest
@ValueSource(strings = { "racecar", "radar", "able was I ere I saw elba" })
public void testCaseUsingParams(String candidate) throws Exception {
}
@Test
public void testCaseWithoutParams() throws Exception {
}
}
If you are still using Junit 4 (I tested with v4.8.2) you can use the Enclosed runner in conjunction with inner classes and the Parameterized runner:
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(Enclosed.class)
public class ComponentTest {
@RunWith(Parameterized.class)
public static class ComponentParamTests {
@Parameters
...
@Test
public void testCaseUsingParams() throws Exception {
}
}
public static class ComponentSingleTests {
@Test
public void testCaseWithoutParams() throws Exception {
}
}
}
Zohhak test runner is a simpler way to parameterize specific tests. Thanks Piotr!
I was able to do something very similar to Matthew Madson answer and found it useful to create a Base Class to encapsulate setup and common helper functions between the single and param tests. This works without using Enclosed.class.
@RunWith(Suite.class)
@SuiteClasses({ComponentTest.ComponentParamTests.class, ComponentTest.ComponentSingleTests.class})
public class ComponentTest {
public static class TestBase {
@Spy
...
@Before
...
}
@RunWith(Parameterized.class)
public static class ComponentParamTests extends TestBase{
@Parameter
...
@Parameters
...
@Test
...
}
public static class ComponentSingleTests extends TestBase{
@Test
...
}
}
I just found out that one can use JUnitParams. I converted one of my tests now to use it and it works beautifully.
I did something similar to Matthew's Solution. However, I created two new java files that extends the current file so that the ComponentSingleTests do not run twice. This way, they can share common member variables and helper methods from the parent class. The problem I had with Matthew's Solution was that my single test was running twice instead of once as the Enclosed.class (which extends the Suite.class) will make your test run twice as described in this link Prevent junit tests from running twice
ComponentTest.java
public class ComponentTest {
public int sharedMemberVariables;
...
}
ComponentParamTests.java
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(Parameterized.class)
public class ComponentParamTests extends ComponentTest {
@Parameters
...
@Test
public void testCaseUsingParams() throws Exception {
}
}
ComponentSingleTests.java
import org.junit.Test;
public class ComponentSingleTests {
@Test
public void testCaseWithoutParams() throws Exception {
...
}
}