1. 程式人生 > >Spring boot + JUnit5 測試

Spring boot + JUnit5 測試

因為《碼出高效 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介面方法沒有