Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run JUnit SpringJUnit4ClassRunner with Parametrized?

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?

like image 648
membersound Avatar asked Feb 17 '15 11:02

membersound


People also ask

What is SpringJUnit4ClassRunner?

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.

What is the difference between SpringJUnit4ClassRunner and SpringRunner?

There is no difference, from the javadoc: SpringRunner is an alias for the SpringJUnit4ClassRunner.

How do I use Mockito with SpringRunner?

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; // ... }

What does @RunWith SpringRunner class mean?

@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.


1 Answers

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();      ... 
like image 110
keyoxy Avatar answered Sep 21 '22 17:09

keyoxy