jeudi 12 décembre 2019

apace cxf tests don't work with embedded Tomcat

I have simple apache cxf jax-rs app, on spring boot. I can't understand, why my tests don't work with embedded Tomcat. My test class:

   @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);
        }
    }


}

It works with jetty perfectly, but now I should do it with Tomcat, and I get this exceptions:

org.apache.http.conn.HttpHostConnectException: Connect to localhost:8000 [localhost/127.0.0.1] failed: Connection refused (Connection refused)

Caused by: java.net.ConnectException: Connection refused (Connection refused)

What I did wrong?

Aucun commentaire:

Enregistrer un commentaire