春季启动时的集成测试方法

时间:2018-06-08 08:02:04

标签: java spring-boot integration-testing

我遇到集成测试问题导致一个元素无效。当我添加

JSONAssert.assertEquals(expected, response.getBody(),false);

我有红色错误和信息:未处理的异常:org.json.JSONException。 我展示了我所有的集成测试代码:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TeamService.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TeamIntegrationTest {

    @LocalServerPort
    private int port;

    TestRestTemplate restTemplate = new TestRestTemplate();

    HttpHeaders headers = new HttpHeaders();

    @Test
    public void integrationTeamTest(){

        HttpEntity<String> entity = new HttpEntity<String>(null,headers);

        HttpEntity<String> response = restTemplate.exchange(
                createURLWithPort("/teams"),
                HttpMethod.GET,entity,String.class);

        String expected = "{\"id\":1,\"name\":\"Apacze\",\"description\":\"grupa programistow\",\"city\":\"Włocławek\",\"headcount\":null},{\"id\":2,\"name\":\"Apacze\",\"description\":\"grupa programistow\",\"city\":\"Radom\",\"headcount\":null},{\"id\":3,\"name\":\"Apacze\",\"description\":\"grupa programistow\",\"city\":\"Warszawa\",\"headcount\":null},{\"id\":4,\"name\":\"Apacze\",\"description\":\"grupa programistow\",\"city\":\"Kraków\",\"headcount\":null}";

        JSONAssert.assertEquals(expected, response.getBody(),false); -> this line throw exception
    }

    private String createURLWithPort(String uri){
        return "http://localhost:" + port + uri;
    }
}

0 个答案:

没有答案