apace cxf测试不适用于嵌入式Tomcat

时间:2019-12-12 19:48:14

标签: spring-boot tomcat testing jax-rs cxf

我在春季启动时有一个简单的Apache cxf jax-rs应用程序。我不明白为什么我的测试不适用于嵌入式Tomcat。我的测试课:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { RestfullServerStarter.class })
public class ServiceImplTests {
    private static final String INFO_ENDPOINT_URL = "api/info";
    private static final String HEALTH_ENDPOINT_URL = "api/health";
    private CloseableHttpClient client;
    private String host = "http://localhost:8000/";

    @Before
    public void createClient() {
        client = HttpClients.createDefault();
    }

    @After
    public void closeClient() throws IOException {
        client.close();
    }   

    @Test
    public void testGetInfoSuccess() throws ClientProtocolException, IOException, ParseException {
        HttpGet httpGet = new HttpGet(host + INFO_ENDPOINT_URL);
        HttpResponse response = client.execute(httpGet);
        assertEquals(200, response.getStatusLine().getStatusCode());

        Info infoFromResponse = getInfoFromResponse();

        assertTrue(isVersionSetCorrectly(infoFromResponse.getVersion()));
        LocalDate date = LocalDate.now();

        assertTrue(infoFromResponse.getBuildTime().contains(date.toString()));
    }

    private boolean isVersionSetCorrectly(String version) {
        Pattern pattern = Pattern.compile("^\\d{1,3}[.]+\\d{1,3}[.]*\\d*[.]*[-]*[a-zA-Z\\d]*");
        Matcher matcher = pattern.matcher(version);

        if (matcher.find()) {
            return true;
        }

        return false;
    }

    @Test
    public void testGetStatusSuccess() throws ClientProtocolException, IOException {
        HttpGet httpGet = new HttpGet(host + HEALTH_ENDPOINT_URL);
        HttpResponse response = client.execute(httpGet);
        assertEquals(200, response.getStatusLine().getStatusCode());

        Status statusFromResponse = getStatusFromResponse();
        assertEquals("OK", statusFromResponse.getStatus());
    }

    private Status getStatusFromResponse() throws IOException {
        final URL url = new URL(host + HEALTH_ENDPOINT_URL);
        HttpGet httpGet = new HttpGet(host + HEALTH_ENDPOINT_URL);
        HttpResponse response = client.execute(httpGet);
        if ("Content-Type: application/xml".equals(response.getFirstHeader("Content-Type").toString())) {
            InputStream input = url.openStream();
            return JAXB.unmarshal(new InputStreamReader(input), Status.class);
        } else {
            ObjectMapper objectMapper = new ObjectMapper();
            InputStream input = url.openStream();
            return objectMapper.readValue(input, Status.class);
        }
    }

    private Info getInfoFromResponse() throws IOException {
        final URL url = new URL(host + INFO_ENDPOINT_URL);
        HttpGet httpGet = new HttpGet(host + INFO_ENDPOINT_URL);
        HttpResponse response = client.execute(httpGet);
        if ("Content-Type: application/xml".equals(response.getFirstHeader("Content-Type").toString())) {
            InputStream input = url.openStream();
            return JAXB.unmarshal(new InputStreamReader(input), Info.class);
        } else {
            ObjectMapper objectMapper = new ObjectMapper();
            InputStream input = url.openStream();
            return objectMapper.readValue(input, Info.class);
        }
    }

}

它可以完美地与码头一起工作,但是现在我应该使用Tomcat进行操作,并且出现以下异常:

  

org.apache.http.conn.HttpHostConnectException:连接到本地主机:8000 [localhost / 127.0.0.1]失败:连接被拒绝(连接被拒绝)

     

原因:java.net.ConnectException:连接被拒绝(连接被拒绝)

0 个答案:

没有答案