版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32768743/article/details/85145915
因为《码出高效 Java开发手册》讲单元测试的时候,用的是JUnit5,而项目中用的是JUnit4,于是做了一下升级。
JUnit5依赖变好多,咋没有一个包解决所有问题呢?
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
也用到了书中推荐的一个断言库
<!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.11.1</version>
<scope>test</scope>
</dependency>
然后在Spring boot的测试中写了一个简单的测试
基类
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment= RANDOM_PORT)
public abstract class BaseTest {
protected String baseUrl;
protected void response(Response response) {
Assertions.assertThat(response).as("非空判断").isNotNull();
Assertions.assertThat(response.getCode()).as("响应码判断").isEqualTo(200);
Assertions.assertThat(response.getData()).as("非空判断").isNotNull();
}
}
我的测试
@DisplayName("代理商接口")
@ExtendWith(SpringExtension.class)
class AgentMainApiControllerTest extends BaseTest {
@Autowired
private TestRestTemplate restTemplate;
@BeforeEach
void setUp() {
baseUrl = "/api/agent";
}
@AfterEach
void tearDown() {
}
@DisplayName("门店概况")
@ParameterizedTest
@CsvSource({
"1535355969521, 6, 0, 0, 0, 0"
})
void getStoreOverview(String agentId,
int onBusinessStoreCount,
int stopBusinessStoreCount,
int saleGoodsCount,
int storeAlarmCount,
int goodsAlarmCount) {
Response<StoreOverviewVO> response;
ParameterizedTypeReference<Response<StoreOverviewVO>> typeRef = new ParameterizedTypeReference<Response<StoreOverviewVO>>() {
};
String url = "/{agentId}/stores/overview";
response = this.restTemplate.exchange(baseUrl+url, GET, null, typeRef, agentId).getBody();
response(response);
StoreOverviewVO data = response.getData();
assertThat(data.getOnBusinessStoreCount()).isEqualTo(onBusinessStoreCount);
assertThat(data.getStopBusinessStoreCount()).isEqualTo(stopBusinessStoreCount);
assertThat(data.getSaleGoodsCount()).isEqualTo(saleGoodsCount);
assertThat(data.getStoreAlarmCount()).isEqualTo(storeAlarmCount);
assertThat(data.getGoodsAlarmCount()).isEqualTo(goodsAlarmCount);
}
@Test
void queryStores() {
}
@DisplayName("所有门店信息")
@ParameterizedTest
@CsvSource({
"1535355969521"
})
void getAllStores(String agentId) {
Response<List<StoreItemDTO>> response;
ParameterizedTypeReference<Response<List<StoreItemDTO>>> typeRef
= new ParameterizedTypeReference<Response<List<StoreItemDTO>>>() {
};
String url = "/{agentId}/stores";
response = this.restTemplate.exchange(baseUrl+url, GET, null, typeRef, agentId).getBody();
response(response);
List<StoreItemDTO> data = response.getData();
assertThat(data.size()).as("门店个数").isEqualTo(6);
}
@Test
void getStoreInfo() {
}
@DisplayName("代理商信息")
@ParameterizedTest
@CsvSource({
"1535355969521, 总代, xxx省, 总代, xxx, xxxxxxxxxxx, 1535270400000, 1535270400000, , , "
})
void getAgentInfo(String agentId,
String agentLevelName,
String area,
String agentName,
String contactName,
String contactPhone,
Long beginTime,
Long endTime,
String businessLice,
String idcardUp,
String idcardDown) {
Response<AgentVO> response;
ParameterizedTypeReference<Response<AgentVO>> typeRef
= new ParameterizedTypeReference<Response<AgentVO>>() {
};
String url = "/{agentId}";
response = this.restTemplate.exchange(baseUrl+url, GET, null, typeRef, agentId).getBody();
response(response);
AgentVO data = response.getData();
assertThat(data.getAgentLevelName()).as("代理商级别").isEqualTo(agentLevelName);
assertThat(data.getArea()).as("代理区域").isEqualTo(area);
assertThat(data.getAgentName()).as("代理商名称").isEqualTo(agentName);
assertThat(data.getContactName()).as("联系人").isEqualTo(contactName);
assertThat(data.getContactPhone()).as("手机号码").isEqualTo(contactPhone);
assertThat(data.getBeginTime()).as("有效期开始时间").isEqualTo(beginTime);
assertThat(data.getEndTime()).as("有效期结束时间").isEqualTo(endTime);
assertThat(data.getBusinessLice()).as("营业执照").isEqualTo(businessLice);
assertThat(data.getIdcardUp()).as("法人代表身份证 上").isEqualTo(idcardUp);
assertThat(data.getIdcardDown()).as("法人代表身份证 下").isEqualTo(idcardDown);
}
@Test
void updateAgent() {
}
}
不知道有什么好的测试REST接口方法没有