The following code is invalid due to duplicate @RunWith
annotation:
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(Parameterized.class) @SpringApplicationConfiguration(classes = {ApplicationConfigTest.class}) public class ServiceTest { }
But how can I use these two annotations in conjunction?
SpringJUnit4ClassRunner is a custom extension of JUnit's BlockJUnit4ClassRunner which provides functionality of the Spring TestContext Framework to standard JUnit tests by means of the TestContextManager and associated support classes and annotations.
There is no difference, from the javadoc: SpringRunner is an alias for the SpringJUnit4ClassRunner.
For example, you can use the Spring runner with the Mockito rule as follows. @RunWith(SpringRunner. class) @SpringBootTest public class MyTests { @Rule public MockitoRule rule = MockitoJUnit. rule(); @Mock MyService myService; // ... }
@RunWith(SpringRunner. class) tells JUnit to run using Spring's testing support. SpringRunner is the new name for SpringJUnit4ClassRunner , it's just a bit easier on the eye.
You can use SpringClassRule and SpringMethodRule - supplied with Spring
import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.springframework.test.context.junit4.rules.SpringClassRule; import org.springframework.test.context.junit4.rules.SpringMethodRule; @RunWith(Parameterized.class) @ContextConfiguration(...) public class MyTest { @ClassRule public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule(); @Rule public final SpringMethodRule springMethodRule = new SpringMethodRule(); ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With