2010-08-04 52 views
14

Tôi đang sử dụng Spring để chèn đường dẫn đến thư mục vào các bài kiểm tra đơn vị của tôi. Bên trong thư mục này là một số tệp cần được sử dụng để tạo dữ liệu thử nghiệm cho các trường hợp thử nghiệm được tham số bằng cách sử dụng runner thử nghiệm Parameterized. Thật không may, Á hậu thử nghiệm yêu cầu phương pháp cung cấp các tham số là tĩnh. Điều này không làm việc cho tình huống của tôi bởi vì thư mục chỉ có thể được tiêm vào một trường không tĩnh. Bất kỳ ý tưởng làm thế nào tôi có thể nhận được xung quanh này?Làm cách nào để sử dụng trình kiểm tra JUnit Parameterized JUnit với trường được tiêm bằng Spring?

+2

Bạn có thể thay đổi sang cơ chế khác để tiêm một chuỗi thành một thử nghiệm vật cố? Tôi có nghĩa là nút cổ chai ở đây có vẻ là mùa xuân không thể thiết lập các lĩnh vực không tĩnh/ – Gishu

+0

Gishu: đặt trường tĩnh, không? Có thể không có trong JUnit vì thứ tự hoặc thực thi sẽ có phương thức @Parameters được gọi trước Spring. Nhưng về tổng thể? –

+0

Liên quan: https://stackoverflow.com/questions/28560734/how-to-run-junit-springjunit4classrunner-with-parametrized – tkruse

Trả lời

6

tôi giả sử bạn đang sử dụng JUnit 4.x kể từ khi bạn đề cập đến Á hậu kiểm tra tham số. Điều này ngụ ý rằng bạn không sử dụng @RunWith (SpringJUnit4ClassRunner). Không phải là một vấn đề, chỉ cần liệt kê các giả định của tôi.

Sau đây sử dụng Spring để lấy thư mục tệp thử nghiệm từ tệp XML. Nó không tiêm nó, nhưng dữ liệu vẫn có sẵn cho thử nghiệm của bạn. Và trong một phương pháp tĩnh không kém.

Điểm bất lợi duy nhất tôi thấy là có thể có nghĩa là cấu hình Spring của bạn sẽ được phân tích cú pháp/định cấu hình nhiều lần. Bạn có thể tải chỉ một tệp nhỏ hơn với thông tin kiểm tra cụ thể nếu cần.

@RunWith(Parameterized.class) 
public class MyTest { 
    @Parameters 
    public static Collection<Object[]> data() { 
     ApplicationContext ctx = new ClassPathXmlApplicationContext("/jeanne/jeanne.xml"); 
     String dir = ctx.getBean("testFilesDirectory", String.class); 

      // write java code to link files in test directory to the array 
     return Arrays.asList(new Object[][] { { 1 } }); 
    } 
// rest of test class 
} 
+0

Ý tưởng hay. Tôi quên hoàn toàn về việc chỉ nhận được 'ApplicationContext' theo cách thủ công. Nó làm cho mã này trở nên xấu hơn một chút, nhưng nó làm cho đầu ra thử nghiệm vô cùng tốt hơn để có thể sử dụng 'Parameterized'. –

0

Hãy nhớ rằng Spring inject sử dụng @Autowired, mà còn với setter. Vì vậy, thay vì sử dụng @Autowired, sử dụng setter:

private static String directory; 

public void setDirectory(String directory) { 
    this.directory = directory; 
} 

public static String getDirectory() { 
    return directory; 
} 
11

Bạn có thể sử dụng TestContextManager từ Spring. Trong ví dụ này, tôi đang sử dụng lý thuyết thay vì tham số.

@RunWith(Theories.class) 
@ContextConfiguration(locations = "classpath:/spring-context.xml") 
public class SeleniumCase { 
    @DataPoints 
    public static WebDriver[] drivers() { 
    return new WebDriver[] { firefoxDriver, internetExplorerDriver }; 
    } 

    private TestContextManager testContextManager; 

    @Autowired 
    SomethingDao dao; 

    private static FirefoxDriver firefoxDriver = new FirefoxDriver(); 
    private static InternetExplorerDriver internetExplorerDriver = new InternetExplorerDriver(); 

    @AfterClass 
    public static void tearDown() { 
    firefoxDriver.close(); 
    internetExplorerDriver.close(); 
    } 

    @Before 
    public void setUpStringContext() throws Exception { 
    testContextManager = new TestContextManager(getClass()); 
    testContextManager.prepareTestInstance(this); 
    } 

    @Theory 
    public void testWork(WebDriver driver) { 
    assertNotNull(driver); 
    assertNotNull(dao); 
    } 
} 

tôi tìm thấy giải pháp này ở đây: How to do Parameterized/Theories tests with Spring

+0

nói rằng bạn cần gắn nhãn thử nghiệm này với chú thích Spring, như @Transactional. Làm thế nào bạn sẽ làm điều đó với TestContextManager? –

3

tôi sử dụng các giải pháp sau đây với Parameterized.class mà không cần bất kỳ vấn đề: http://bmocanu.ro/coding/320/combining-junit-theoriesparameterized-tests-with-spring/

@ContextConfiguration(value = "classpath:test-context.xml") 
public abstract class AbstractJunitTest extends AbstractJUnit4SpringContextTests { 
    private static TestContextManager testContextManager = null; 
    private static DAOFactory daoFactory = null; 

    @Before 
    public void initApplicationContext() throws Exception { 
     if (testContextManager == null) { 
      testContextManager = new TestContextManager(getClass()); 
      testContextManager.prepareTestInstance(this); 
      daoFactory = (DAOFactory)applicationContext.getBean("daoFactory"); 
     } 
    } 

    protected DAOFactory getDaoFactory() throws Exception { 
     return daoFactory; 
    } 
} 



@RunWith(Parameterized.class) 
public class SomeTestClass extends AbstractJunitTest { 
    ... 
} 
4

Nó đủ để chú thích lớp thử nghiệm với @RunWith(Parameterized.class)@ContextConfiguration, sử dụng @Autowired để tiêm phụ thuộc và sử dụng TestContextManager trong hàm khởi tạo để khởi tạo, ví dụ:

@RunWith(Parameterized.class) 
@ContextConfiguration(classes = TestConfig.class) 
public class MyTest { 

    @Autowired 
    private DataSource dataSource; 

    private final int param; 

    @Parameterized.Parameters 
    public static List<Object[]> params() { 
     return Arrays.asList(new Object[][]{ 
      {1}, 
      {2}, 
     }); 
    } 

    public MyTest(int p) { 
     this.param = p; 
     new TestContextManager(getClass()).prepareTestInstance(this); 
    } 

    @Test 
    public void testSomething() { 
     … 
    } 
} 
+0

Như một người đứng đầu, hãy nhớ rằng bạn không thể sử dụng đậu được tiêm (dataSource trong ví dụ này) làm nguồn tham số. @ Parameterized.Parameters phương pháp cần phải được tĩnh. –

3

Đối với một người nào đó đọc cuối năm 2015 này hay muộn, Spring 4.2 đã, ngoài SpringJUnit4ClassRunner thêm SpringClassRule và SpringMethodRule mà đòn bẩy hỗ trợ cho Spring TestContext Framework.

Điều này có nghĩa hỗ trợ lớp học đầu tiên đối với bất kỳ Runner như MockitoJUnitRunner hoặc Parameterized:

@RunWith(Parameterized.class) 
public class FibonacciTest { 
    @ClassRule public static final SpringClassRule SCR = new SpringClassRule(); 
    @Rule public final SpringMethodRule springMethodRule = new SpringMethodRule(); 

    long input; 
    long output; 

    public FibonacciTest(long input, long output) { this.input = input; ...} 

    @Test 
    public void testFibonacci() { 
     Assert.assertEquals(output, fibonacci(input)); 
    } 

    public List<Long[]> params() { 
     return Arrays.asList(new Long[][] { {0, 0}, {1, 1} }); 
    } 
} 
Các vấn đề liên quan