javascript
junit测试spring_使用Spring JUnit规则进行参数化集成测试
junit測(cè)試spring
Spring 4.2附帶了全新的JUnit規(guī)則: SpringClassRule和SpringMethodRule 。 使用JUnit規(guī)則的主要優(yōu)點(diǎn)是讓開發(fā)人員擺脫SpringJUnit4ClassRunner并在Spring集成測(cè)試中利用不同的JUnit運(yùn)行器。 我認(rèn)為Spring JUnit Rules的最大機(jī)會(huì)是易于創(chuàng)建參數(shù)化的集成測(cè)試。
要測(cè)試的代碼
出于本文的目的,我使用了現(xiàn)有的Spring Boot Jersey Demo應(yīng)用程序: https : //github.com/kolorobot/spring-boot-jersey-demo 。 該應(yīng)用程序公開了簡(jiǎn)單的REST API以與客戶對(duì)象一起使用。
集成測(cè)試–“舊”方式
在Spring 4.2之前,集成測(cè)試可能如下所示:
@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是一個(gè)分組注釋,它包裝了幾個(gè)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 {}您可能會(huì)注意到,以上測(cè)試使用標(biāo)準(zhǔn)SpringJUnit4ClassRunner ,這是一個(gè)自定義運(yùn)行器,在JUnit集成測(cè)試中增加了Spring Framework的支持。 而且由于不能在JUnit中使用多個(gè)運(yùn)行程序,所以我們需要找到一種變通方法來(lái)使用Spring和JUnitParams創(chuàng)建參數(shù)化測(cè)試(這并不是很困難的BTW)。
使用Spring JUnit規(guī)則進(jìn)行參數(shù)化測(cè)試
值得一提的是,Spring 4.2附帶了SpringJUnit4ClassRunner一個(gè)便捷替代SpringJUnit4ClassRunner :Spring JUnit Rules。 讓我們來(lái)看一個(gè)例子:
@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"));} }原始代碼沒(méi)有太多更改,但是最重要的是:
- JUnitParamsRunner – JUnitParams是標(biāo)準(zhǔn)JUnit參數(shù)化測(cè)試的替代方法。 我在這里寫過(guò)博客: http : //blog.codeleak.pl/2013/12/parametrized-junit-tests-with.html和此處: http : //blog.codeleak.pl/2014/11/unit-testing- excercise-with-fizzbuzz.html 。
- SpringClassRule -支持的類級(jí)別功能SpringJUnit4ClassRunner ,必須用相結(jié)合SpringMethodRule 。 字段的名稱無(wú)關(guān)緊要,但它必須是公共的,靜態(tài)的和最終的。
- SpringMethodRule -支持的實(shí)例級(jí)和方法級(jí)設(shè)有SpringJUnit4ClassRunner因此,它必須與組合SpringClassRule
- @Parameters –測(cè)試參數(shù)的注釋。 默認(rèn)情況下,需要parametersFor<methodName>方法。
使用gradle test --tests *SaveCustomerParameterizedTest運(yùn)行測(cè)試將產(chǎn)生以下報(bào)告:
如您所見,執(zhí)行了4個(gè)測(cè)試。 第一個(gè)花費(fèi)了大部分時(shí)間,因?yàn)槌跏蓟薙pring上下文,后面的測(cè)試非常快。
摘要
在Spring Test Framework中添加Spring JUnit規(guī)則可以顯著改善集成測(cè)試,尤其是在進(jìn)行參數(shù)化測(cè)試時(shí)。 但是,不僅JUnitParams可以用于此目的。 您也可以嘗試使用標(biāo)準(zhǔn)的JUnit org.junit.runners.Parameterized 。
資源資源
- Spring框架參考-http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#testcontext-junit4-rules
- 使用JUnitParams進(jìn)行參數(shù)化的JUnit測(cè)試– http://blog.codeleak.pl/2013/12/parametrized-junit-tests-with.html
- JUnitParams – https://github.com/Pragmatists/JUnitParams
- 使用FizzBu??zz和JUnitParams進(jìn)行單元測(cè)試的練習(xí)– 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
junit測(cè)試spring
總結(jié)
以上是生活随笔為你收集整理的junit测试spring_使用Spring JUnit规则进行参数化集成测试的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 大禹科技有限公司(大禹科技股份有限公司)
- 下一篇: 属鸡的今年多大了2021(不同出生年份年