simple-java icon indicating copy to clipboard operation
simple-java copied to clipboard

Integration tests

Open nikolaykuz opened this issue 10 years ago • 0 comments

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

nikolaykuz avatar Mar 14 '15 14:03 nikolaykuz