javascript
使用Spring JUnit规则进行参数化集成测试
Spring 4.2附帶了全新的JUnit規(guī)則: SpringClassRule和SpringMethodRule 。 使用JUnit規(guī)則的主要優(yōu)點是讓開發(fā)人員擺脫SpringJUnit4ClassRunner并在Spring集成測試中利用不同的JUnit運行器。 我認為Spring JUnit Rules的最大機會是易于創(chuàng)建參數(shù)化的集成測試。
要測試的代碼
出于本文的目的,我使用了現(xiàn)有的Spring Boot Jersey Demo應用程序: https : //github.com/kolorobot/spring-boot-jersey-demo 。 該應用程序公開了簡單的REST API以與客戶對象一起使用。
集成測試–“舊”方式
在Spring 4.2之前,集成測試可能如下所示:
@RunWith(SpringJUnit4ClassRunner.class) @ApplicationTest public class SaveCustomerTest {private RestTemplate restTemplate = new TestRestTemplate("demo", "123");@Testpublic void savesCustomer() {// actURI uri = restTemplate.postForLocation("http://localhost:9000/customer",new Customer("John", "Doe"));// assertResponseEntity<Customer> responseEntity =restTemplate.getForEntity(uri, Customer.class);Customer customer = responseEntity.getBody();assertThat(customer.getFirstname()).isEqualTo("John");assertThat(customer.getLastname()).isEqualTo("Doe");} }@ApplicationTest是一個分組注解,它包裝了多個Spring的注解:
@Documented @Inherited @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration @org.springframework.boot.test.IntegrationTest("server.port=9000") @ActiveProfiles("web") @Sql(scripts = "classpath:data.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) public @interface ApplicationTest {}您可能會注意到,以上測試使用標準SpringJUnit4ClassRunner ,這是一個自定義運行程序,在JUnit集成測試中增加了Spring Framework的支持。 并且由于不能在JUnit中使用多個運行程序,因此我們需要找到一種變通方法,以使用Spring和JUnitParams創(chuàng)建參數(shù)化測試(這并不是那么困難)。
使用Spring JUnit規(guī)則進行參數(shù)化測試
值得一提的是,Spring 4.2隨附了SpringJUnit4ClassRunner的便捷替代SpringJUnit4ClassRunner :Spring JUnit Rules。 讓我們來看一個例子:
@RunWith(JUnitParamsRunner.class) @ApplicationTest public class SaveCustomerParameterizedTest {@ClassRulepublic static final SpringClassRule SCR = new SpringClassRule();@Rulepublic final SpringMethodRule springMethodRule = new SpringMethodRule();private RestTemplate restTemplate = new TestRestTemplate("demo", "123");@Test@Parameterspublic void savesCustomer(String first, String last) {// actURI uri = restTemplate.postForLocation("http://localhost:9000/customer",new Customer(first, last));// assertResponseEntity<Customer> responseEntity =restTemplate.getForEntity(uri, Customer.class);Customer customer = responseEntity.getBody();assertThat(customer.getFirstname()).isEqualTo(first);assertThat(customer.getLastname()).isEqualTo(last);}public Object[] parametersForSavesCustomer() {return $($("John", "Doe"),$("John", "Smith"),$("Deborah", "Johnson"),$("Jan", "Kowalski"));} }原始代碼沒有太多更改,但最重要的是:
- JUnitParamsRunner – JUnitParams是標準JUnit參數(shù)化測試的替代方法。 我在這里寫過博客: http : //blog.codeleak.pl/2013/12/parametrized-junit-tests-with.html和此處: http : //blog.codeleak.pl/2014/11/unit-testing- excercise-with-fizzbuzz.html 。
- SpringClassRule -支持的類級別功能SpringJUnit4ClassRunner ,必須用相結合SpringMethodRule 。 字段的名稱無關緊要,但它必須是公共的,靜態(tài)的和最終的。
- SpringMethodRule -支持的實例級和方法級設有SpringJUnit4ClassRunner因此,它必須與組合SpringClassRule
- @Parameters –測試參數(shù)的注釋。 默認情況下,需要parametersFor<methodName>方法。
使用gradle test --tests *SaveCustomerParameterizedTest運行測試將生成以下報告:
如您所見,執(zhí)行了4個測試。 第一個花費了大部分時間,因為初始化了Spring上下文,后面的測試非常快。
摘要
在Spring Test Framework中添加Spring JUnit規(guī)則可以顯著改善集成測試,尤其是在進行參數(shù)化測試時。 但是,不僅JUnitParams可以用于此目的。 您也可以嘗試使用標準的JUnit org.junit.runners.Parameterized 。
資源資源
- Spring框架參考-http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#testcontext-junit4-rules
- 使用JUnitParams進行參數(shù)化的JUnit測試– http://blog.codeleak.pl/2013/12/parametrized-junit-tests-with.html
- JUnitParams – https://github.com/Pragmatists/JUnitParams
- 使用FizzBu??zz和JUnitParams進行單元測試的練習– http://blog.codeleak.pl/2014/11/unit-testing-excercise-with-fizzbuzz.html
翻譯自: https://www.javacodegeeks.com/2015/08/parameterized-integration-tests-with-spring-junit-rules.html
總結
以上是生活随笔為你收集整理的使用Spring JUnit规则进行参数化集成测试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dom4j和jaxb_JAXB,SAX,
- 下一篇: 使用Spring AOP和Guava速率