Groovier Spring Boot集成测试

本文介绍如何在Spring Boot项目中使用Groovy进行集成测试,包括添加Groovy依赖、使用RESTClient进行API测试及验证响应内容的方法。

最近,我有机会在soapUI REST项目中使用Groovy的groovy.json.JsonSlurper 。 soapUI(特别是soapUI Open Source)的使用场景非常简单:在Groovy断言中,将响应的内容加载到变量中,然后像这样进行验证:

import groovy.json.JsonSlurper;

def slurper = new JsonSlurper()
def result = slurper.parseText(messageExchange.responseContent)

assert result.incidentType == 'Lorem ipsum'

这在soapUI中效果很好。 但是, Spring Boot集成测试如何? 我可以对REST API Groovier进行集成测试吗?

需要测试的API

出于本文的目的,我使用了现有的Spring Boot Jersey Demo应用程序: https : //github.com/kolorobot/spring-boot-jersey-demo

将Groovy添加到Spring Boot项目

添加Groovy和HTTPBuilder(Groovy的简易HTTP客户端)依赖项。

testCompile 'org.codehaus.groovy:groovy-all:2.4.3'
testCompile 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'

所有Groovy测试都将保存在src/test/groovy文件夹中。

首次集成测试

第一个测试将验证/customer端点:

@RunWith(SpringJUnit4ClassRunner.class)
@ApplicationTest
class FindAllCustomersGroovyTest {

    RESTClient client = new RESTClient("http://localhost:9000")

    @Before
    def void before() {
        client.auth.basic("demo", "123")
    }


    @Test
    def void findsAll() {
        def response = client.get(path: "/customer")

        def json = response.responseData

        assert json.number == 0
        assert json.totalPages == 1
        assert json.totalElements == 3
        assert json.content.size() == 3
        assert json.content.firstname.any() {
            firstname ->
                firstname.equals("Boyd")
                firstname.equals("Carter")
                firstname.equals("Dave")
        }
    }

}
  • @ApplicationTest –是一个分组注释,包装了多个Spring的注释。 Groovy测试可以使用任何JUnit和/或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 {

}
  • HTTPBuilder模块提供了RESTClient扩展,可以轻松查询REST API。 将使用HTTPBuilder代替Spring Boot的TestRestClient
  • API需要基本身份验证,并且使用RESTClient对其进行配置非常简单。 对于更复杂的场景,它允许使用更复杂的身份验证机制。
  • RESTClient响应包含responseData,它是JSONSlurper解析的对象。 而且由于我们很时髦,所以断言响应的内容非常容易。

发送JSON内容

让我们看看如何将JSON数据与请求一起发送:

@RunWith(SpringJUnit4ClassRunner.class)
@ApplicationTest
class SaveCustomerGroovyTest {

    @Test
    public void savesCustomer() {
        def response = client.post(
                path: '/customer',
                requestContentType: "application/json",
                body: [
                        firstname: "John",
                        lastname: "Doe",
                        emailAddress:
                                [value: "john@dummy.com"]
                ]
        )

        assert 201 == response.status
        assert response.headers.location
    }

}

注意:我相信,无需进一步说明。

翻译自: https://www.javacodegeeks.com/2015/05/groovier-spring-boot-integration-testing.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值