2015-02-17 10 views
36

Il seguente codice è valido a causa di duplicare @RunWith annotazione:Come eseguire JUnit SpringJUnit4ClassRunner con Parametrized?

@RunWith(SpringJUnit4ClassRunner.class) 
@RunWith(Parameterized.class) 
@SpringApplicationConfiguration(classes = {ApplicationConfigTest.class}) 
public class ServiceTest { 
} 

Ma come posso utilizzare queste due annotazioni in congiunzione?

+1

Ecco un altro - e fino ad oggi - soluzione http://blog.codeleak.pl/2015/08/parameterized-integration-tests- with.html –

risposta

22

ci sono almeno 2 opzioni per farlo:

  1. seguito http://www.blog.project13.pl/index.php/coding/1077/runwith-junit4-with-both-springjunit4classrunner-and-parameterized/

    vostro test deve essere simile a questa:

    @RunWith(Parameterized.class) 
    @ContextConfiguration(classes = {ApplicationConfigTest.class}) 
    public class ServiceTest { 
    
        private TestContextManager testContextManager; 
    
        @Before 
        public void setUpContext() throws Exception { 
         //this is where the magic happens, we actually do "by hand" what the spring runner would do for us, 
         // read the JavaDoc for the class bellow to know exactly what it does, the method names are quite accurate though 
         this.testContextManager = new TestContextManager(getClass()); 
         this.testContextManager.prepareTestInstance(this); 
        } 
        ... 
    } 
    
  2. C'è un progetto github https://github.com/mmichaelis/spring-aware-rule , che si basa sul precedente blog, ma aggiunge il supporto in modo generalizzato

    @SuppressWarnings("InstanceMethodNamingConvention") 
    @ContextConfiguration(classes = {ServiceTest.class}) 
    public class SpringAwareTest { 
    
        @ClassRule 
        public static final SpringAware SPRING_AWARE = SpringAware.forClass(SpringAwareTest.class); 
    
        @Rule 
        public TestRule springAwareMethod = SPRING_AWARE.forInstance(this); 
    
        @Rule 
        public TestName testName = new TestName(); 
    
        ... 
    } 
    

Quindi è possibile avere una classe base che implementa uno degli approcci e tutti i test che ne ereditano.

+0

Grande, e con il primo modo puoi quindi usare '@ Autowired' su un componente e verrà iniettato correttamente. Inoltre, potrei usare '@ SpringApplicationConfiguration' invece di' @ ContextConfiguration' e ha funzionato bene, non so quale sia la differenza ... – nyg

39

È possibile utilizzare SpringClassRule e SpringMethodRule - fornito con Primavera

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(); 

    ... 
+6

Questa dovrebbe essere la nuova risposta corretta – sjngm

+0

@keyoxy È possibile eseguire tutti i test in parallelo? – gstackoverflow

Problemi correlati