Skip to content

Integration tests #3

Description

@nikolaykuz

Hello Anton,

Thanks for your talks. I really enjoy watching them.

How would you suggest to write integration tests for simple-java project?
I come with this piece of code. Jetty in memory server gets started and request towards a resource is performed. I wonder what you actually think about this approach.

publicclassJettyTest {
privateServerserver;
privateWebTargettarget;
@BeforepublicvoidsetUp() throwsException {
server = newServer(8080);
server.setStopAtShutdown(true);
WebAppContextcontext = newWebAppContext("webapp", "/");
server.setHandler(context);
server.start();
target = ClientBuilder.newClient().target(server.getURI());
}
@AfterpublicvoidtearDown() throwsException {
server.stop();
}
@Testpublicvoidtest() throwsException {
StringresponseMsg = target.path("rest/photo-spots/ids/0").request().get(String.class);
PhotoSpotphotoSpot = target.path("rest/photo-spots/ids/0").request().get(PhotoSpot.class);
assertEquals("Wrong name", "Kohtuotsa vaateplatvorm", photoSpot.getName());
assertEquals("Photospots do not match", serialize(photoSpot), responseMsg);
}
privateStringserialize(Objectobject) throwsException {
JAXBContextcontext = JAXBContext.newInstance(object.getClass());
Marshallermarshaller = context.createMarshaller();
StringWriterstringWriter = newStringWriter();
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
marshaller.marshal(object, stringWriter);
returnstringWriter.toString();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions