2015-02-17 13 views
36

Các mã sau đây là vô hiệu do trùng lặp @RunWith chú thích:Làm thế nào để chạy JUnit SpringJUnit4ClassRunner với Parametrized?

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

Nhưng làm thế nào tôi có thể sử dụng hai chú thích những kết hợp?

+1

Dưới đây là một - và đến nay - giải pháp http://blog.codeleak.pl/2015/08/parameterized-integration-tests- with.html –

Trả lời

22

Có ít nhất 2 lựa chọn để làm điều đó:

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

    thử nghiệm của bạn cần phải tìm kiếm một cái gì đó như thế này:

    @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ó một dự án github https://github.com/mmichaelis/spring-aware-rule , được xây dựng trên blog trước đó, nhưng thêm hỗ trợ theo cách tổng quát

    @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(); 
    
        ... 
    } 
    

Vì vậy, bạn có thể có một lớp cơ bản thực hiện một trong các cách tiếp cận và tất cả các thử nghiệm kế thừa từ đó.

+0

Tuyệt vời và với cách đầu tiên bạn có thể sử dụng '@ Autowired' trên một thành phần và nó sẽ được tiêm chính xác. Ngoài ra, tôi có thể sử dụng '@ SpringApplicationConfiguration' thay vì' @ ContextConfiguration' và nó hoạt động tốt, không chắc chắn sự khác biệt là gì ... – nyg

39

Bạn có thể sử dụng SpringClassRule và SpringMethodRule - cung cấp với mùa xuân

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

Đây phải là câu trả lời đúng mới – sjngm

+0

@keyoxy Có thể chạy tất cả các thử nghiệm song song không? – gstackoverflow

Các vấn đề liên quan