因为《码出高效 Java开发手册》讲单元测试的时候,用的是JUnit5,而项目中用的是JUnit4,于是做了一下升级。
JUnit5依赖变好多,咋没有一个包解决所有问题呢?
org.junit.platform
junit-platform-launcher
1.3.2
test
org.junit.jupiter
junit-jupiter-engine
5.3.2
test
org.junit.vintage
junit-vintage-engine
5.3.2
test
org.junit.jupiter
junit-jupiter-api
5.3.2
test
org.junit.jupiter
junit-jupiter-params
5.3.2
test
也用到了书中推荐的一个断言库
org.assertj
assertj-core
3.11.1
test
然后在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 response;
ParameterizedTypeReference> typeRef = new ParameterizedTypeReference>() {
};
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> response;
ParameterizedTypeReference>> typeRef
= new ParameterizedTypeReference>>() {
};
String url = "/{agentId}/stores";
response = this.restTemplate.exchange(baseUrl+url, GET, null, typeRef, agentId).getBody();
response(response);
List 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 response;
ParameterizedTypeReference> typeRef
= new ParameterizedTypeReference>() {
};
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接口方法没有