业务场景描述:盘库操作中需要使用具备有效期限的动态码登录系统,接下来是结合 动态码更新API 的单元测试,讲解Assertions 的常规用法。
项目采用 Spring Boot + Gradle 集成,下面使用gradle 语法引入依赖:
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.1.2.RELEASE'
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.11.1'
Jar依赖可访问 maven repository 查找和选取。点击 Jar 依赖查找明细,可看到其支持多种集成工具,如maven,gradle,ant等
import com.fasterxml.jackson.databind.JsonNode;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.transaction.annotation.Transactional;
import static net.javacrumbs.jsonunit.fluent.JsonFluentAssert.assertThatJson;
/**
* @Description 更新动态码
* @Author blake
* @Date 2019-01-22 11:32
* @Version 1.0
*/
@RunWith(SpringRunner.class)
@WebAppConfiguration
@SpringBootTest
@Transactional
@Sql("classpath:/sql/admin/dynamicCode/updateDynamicCodeTests.sql")
public class UpdateDynamicCodeTests extends BaseDynamicCodeControllerTests {
@Autowired
private DynamicCodeService dynamicCodeService;
/**
* 动态码更新-正常态
*/
@Test
public void updateDynamicCodeSuccessfully() throws Exception {
Long id = LongUtil.toLong(1);
// 更新前的动态码信息
DynamicCodeResponse originalDynamicCode = dynamicCodeService.getDynamicCodeInfo(id);
String responseContent = mMockMvc.perform(
MockMvcRequestBuilders.put(BASE_HOST + "/api/admin/dynamic/code/" + id)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.sessionAttr(AUTHORIZED_ATTRIBUTE_NAME, getAuthSession()))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn().getResponse().getContentAsString();
prettyPrintJson(responseContent);
//assert response
JsonNode jsonNode = mObjectMapper.readTree(responseContent);
// 更新成功后的动态码信息
DynamicCodeResponse dynamicCode = jsonNode.get("data").traverse(mObjectMapper).readValueAs(DynamicCodeResponse.class);
assertThatJson(responseContent).node("code").isEqualTo(0);
assertThatJson(responseContent).node("data").isNotEqualTo(null);
assertThatJson(responseContent).node("message").isEqualTo("");
assertThatJson(responseContent).node("status").isStringEqualTo("success");
Assertions.assertThat(originalDynamicCode).isNotEqualTo(null);
Assertions.assertThat(dynamicCode).isNotEqualTo(originalDynamicCode);
Assertions.assertThat(dynamicCode.getActive()).isEqualTo(originalDynamicCode.getActive());
Assertions.assertThat(dynamicCode.getPlanId()).isEqualTo(originalDynamicCode.getPlanId());
Assertions.assertThat(dynamicCode.getCode()).isNotEqualTo(originalDynamicCode.getCode());
Assertions.assertThat(dynamicCode.getIsSub()).isEqualTo(originalDynamicCode.getIsSub());
Assertions.assertThat(dynamicCode.getId()).isEqualTo(originalDynamicCode.getId());
}
}
接下来解释下测试类上标注的5个注解的作用:
- @RunWith(SpringRunner.class) 表明单元测试运行借助于SpringRunner 驱动器
- @WebAppConfiguration 表明这是一个Web类型的单元测试,即Controller级别的Tests
- @SpringBootTest 表明这是一个Spring Boot 项目的单元测试
- @Transactional 事务回滚
- @Sql("classpath:/sql/admin/dynamicCode/updateDynamicCodeTests.sql") 单元测试的数据准备,建议每个table的insert操作前,都使用 truncate table tableName; 将数据清空,以免发生主键冲突等状况。建议连接本地MySQL数据库进行单元测试!