Java - Spring Boot 单元测试

2020-06-01  本文已影响0人  西半球_

ControllerTest

@SpringBootTest(properties = "shiro.enabled = false", webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class GatewayChannelControllerTest {

    @LocalServerPort
    int port;

    private TestRestTemplate restTemplate;

    @MockBean
    GatewayChannelService gatewayChannelService;

    private String channelUrl;
    private String channelEnableUrl;
    private String id;

    @BeforeEach
    public void init() {
        if (channelUrl == null) {
            channelUrl = "http://localhost:8080/v1/api/gatewayChannel";
            channelUrl = "http://localhost:" + port + "/v1/api/gatewayChannel";
        }
        if (channelEnableUrl == null) {
            channelEnableUrl = "http://localhost:8080/v1/api/channel";
            channelEnableUrl = "http://localhost:" + port + "/v1/api/channel";
        }
        if (restTemplate == null) {
            restTemplate = new TestRestTemplate();
        }
        if (id == null) {
            id = "123456";
        }

    }

    @Test
    void addGatewayChannel() throws Exception {



        MultiValueMap<String,Object> mParams = new LinkedMultiValueMap<>();
        mParams.add("metaChannelId", 1);
        mParams.add("gatewayId", "1");
        mParams.add("comno", "comno_新增");
        mParams.add("model", "model");
        mParams.add("driver", "driver");
        mParams.add("baud", 1);
        mParams.add("parity", "parity");
        mParams.add("databit", "databit");
        mParams.add("stopbit", "1");
        mParams.add("communicateInterval", 1);
        mParams.add("maxRetry", 1);
        mParams.add("timeout", 1);
        mParams.add("enabled", true);

        GatewayChannel gatewayChannel1 = GatewayChannel.builder().id("1111111").build(); //返回结果

        GatewayChannel gatewayChannel = GatewayChannel.builder().metaChannelId(1).gatewayId("1").comno("comno_新增").model("model").
                driver("driver").baud(1).parity("parity").databit("databit").stopbit("1").communicateInterval(1)
                .maxRetry(1).timeout(1).enabled(true).build();

        given(gatewayChannelService.addGatewayChannel(gatewayChannel)).willReturn(gatewayChannel1);

        HttpResult response = restTemplate.postForObject(channelUrl, mParams, HttpResult.class);
        assertEquals(0, response.getCode(),response.getMessage());


        /********************************* 不走controller ********************************/
//        given(gatewayChannelService.addGatewayChannel(gatewayChannel)).willReturn(gatewayChannel1);
//        HttpResult response = restTemplate.postForObject(channelUrl, gatewayChannel, HttpResult.class);
//        assertEquals(0, response.getCode(),response.getMessage());
        /********************************* 不走controller ********************************/

    }

    @Test
    void deleteDictTypeById() throws Exception {
        given(gatewayChannelService.deleteGatewayChannelById(id)).willReturn(true);
        restTemplate.delete(channelUrl+"/"+id, HttpResult.class);
    }

    @Test
    void updateGatewayChannelById() throws Exception {
        MultiValueMap<String,Object> mParams = new LinkedMultiValueMap<>();
        mParams.add("comno", "comno_修改");
        mParams.add("model", "model");
        mParams.add("driver", "driver");
        mParams.add("baud", 1);
        mParams.add("parity", "parity");
        mParams.add("databit", "databit");
        mParams.add("stopbit", "1");
        mParams.add("communicateInterval", 1);
        mParams.add("maxRetry", 1);
        mParams.add("timeout", 1);
        mParams.add("enabled", true);

        GatewayChannel gatewayChannel = GatewayChannel.builder().id(id).comno("comno_修改").model("model").driver("driver").baud(1)
                .parity("parity").databit("databit").stopbit("1").communicateInterval(1).maxRetry(1).timeout(1).enabled(true).build();

        given(gatewayChannelService.updateGatewayChannelById(gatewayChannel)).willReturn(gatewayChannel);

        HttpResult response = restTemplate.postForObject(channelUrl+"/"+id, mParams, HttpResult.class);
        assertEquals(0, response.getCode(),response.getMessage());
    }

    @Test
    void enable() throws Exception {
        MultiValueMap<String,Object> mParams = new LinkedMultiValueMap<>();
        mParams.add("enabled", true);
        given(gatewayChannelService.enable(id,true)).willReturn(true);
        HttpResult response = restTemplate.postForObject(channelEnableUrl+"/"+id+"/enable", mParams, HttpResult.class);
        assertEquals(0, response.getCode(),response.getMessage());
    }

    @Test
    void getGatewayChannelList() throws Exception {
        ResponseEntity<HttpResult> response  = restTemplate.getForEntity(channelUrl+"s?gatewayId=1", HttpResult.class);
        assertEquals(0, response.getBody().getCode(),response.getBody().getMessage());
    }

}


ServiceTest


@RunWith(SpringRunner.class)
@SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class GatewayChannelServiceTest {

    @Autowired
    GatewayChannelService gatewayChannelService;

    private String id="123456";

    @BeforeAll
    void prepareTestData() {
    }

    @AfterAll
    void cleanupTestData() {
    }

    @Test
    @Transactional
    public void addGatewayChannel(){
        GatewayChannel gatewayChannel = GatewayChannel.builder().metaChannelId(1).gatewayId("1").comno("service_新增").model("model").driver("driver").baud(1).parity("parity")
           .databit("databit").stopbit("1").communicateInterval(1).maxRetry(1).timeout(1).enabled(true).build();
        GatewayChannel gatewayChannel2= gatewayChannelService.addGatewayChannel(gatewayChannel);
        assertNotEquals(gatewayChannel2,null);
    }

    @Test
    @Transactional
    public void deleteGatewayChannelById(){
        Boolean result = gatewayChannelService.deleteGatewayChannelById(id);
        assertEquals(result,true);
    }

    @Test
    @Transactional
    public void updateGatewayChannelById(){
        GatewayChannel gatewayChannel = GatewayChannel.builder().id(id).comno("service_修改").model("model").driver("driver").baud(1).parity("parity")
                .databit("databit").stopbit("1").communicateInterval(1).maxRetry(1).timeout(1).enabled(true).build();
        GatewayChannel gatewayChannel2= gatewayChannelService.updateGatewayChannelById(gatewayChannel);
        assertNotEquals(gatewayChannel2,null);
    }

    @Test
    @Transactional
    public void enable() {
        Boolean result = gatewayChannelService.enable(id,true);
        assertEquals(result,true);
    }

    @Test
    public void getGatewayChannelList(){
        List<GatewayChannel> lists=gatewayChannelService.getGatewayChannelList(id);
        assertTrue(lists.size()>0);
    }
}

上一篇下一篇

猜你喜欢

热点阅读