Skip to content

Integration tests #3

@nikolaykuz

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.

public class JettyTest {
    private Server server;
    private WebTarget target;

    @Before
    public void setUp() throws Exception {
        server = new Server(8080);
        server.setStopAtShutdown(true);
        WebAppContext context = new WebAppContext("webapp", "/");
        server.setHandler(context);
        server.start();
        target = ClientBuilder.newClient().target(server.getURI());
    }

    @After
    public void tearDown() throws Exception {
        server.stop();
    }

    @Test
    public void test() throws Exception {
        String responseMsg = target.path("rest/photo-spots/ids/0").request().get(String.class);
        PhotoSpot photoSpot = 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);

    }

    private String serialize(Object object) throws Exception {
        JAXBContext context = JAXBContext.newInstance(object.getClass());
        Marshaller marshaller = context.createMarshaller();
        StringWriter stringWriter = new StringWriter();
        marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
        marshaller.marshal(object, stringWriter);
        return stringWriter.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