0

I've been trying to start and stop Jetty automatically in mvn clean install phase for my unit tests. The annoying thing is when I deploy my war file in JBoss all the tests are working fine, however, when I try to use maven-jetty-plugin I have a Connection refuse: connect error. I'm sure about that Jetty starts but doesnt recognize the URLs so that it can't establish the connection.

pom.xml

        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>8.1.13.v20130916</version>
            <configuration>
                <webApp>
                    <contextPath>/</contextPath>
                </webApp>
                <scanIntervalSeconds>10</scanIntervalSeconds>
                <stopKey>foo</stopKey>
                <stopPort>9999</stopPort>
            </configuration>
            <executions>
                <execution>
                    <id>start-jetty</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <scanIntervalSeconds>0</scanIntervalSeconds>
                        <daemon>true</daemon>
                    </configuration>
                </execution>
                <execution>
                    <id>stop-jetty</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

default web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      version="3.0">
</web-app>

and my other classes

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

import com.buraktas.resource.DefaultBookResource;

@ApplicationPath("/services")
public class BaseApplication extends Application {

    private Set<Object> singletons = new HashSet<Object>();

    public BaseApplication() {

        singletons.add(new DefaultBookResource());
    }

    @Override
    public Set<Object> getSingletons() {
        return singletons;
    }
}

entity class

public class BookEntity {

private int    id;
private String title;
private String author;
private double price;

... setters and getters

}

resource interface

@Path("/book-service")
public interface BookResource {

    @POST
    @Path("/create")
    @Consumes("application/json")
    @Produces("application/json")
    public BookEntity createBook(BookEntity bookEntity);

}

and the implementation of it

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import com.buraktas.entity.BookEntity;

public class DefaultBookResource implements BookResource {

    private Map<Integer, BookEntity> list      = new HashMap<Integer, BookEntity>();
    private AtomicInteger            idCounter = new AtomicInteger();

    @Override
    public BookEntity createBook(BookEntity bookEntity) {

        bookEntity.setId(idCounter.getAndIncrement());
        list.put(bookEntity.getId(), bookEntity);

        return bookEntity;
    }

}

finally test class

public class TestClass {

    private static final String POST_INPUT = "{\"title\" : \"LOTR\",\"author\" : \"J.K.Rowling\",\"price\" : 12.99}";
    private static Client       client;

    @Before
    public void setUp() {

        ClientConfig clientConfig = new DefaultClientConfig();
        clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
        client = Client.create(clientConfig);
    }

    @After
    public void close() {

        client.destroy();
    }

    @Test
    public void testCreateSuccess() throws Exception {

        WebResource webResourcePost = client.resource("http://localhost:8080/jersey-rest-client/services/book-service/create");
        ClientResponse response = webResourcePost.accept("application/json").type("application/json").post(ClientResponse.class, POST_INPUT);
        BookEntity responseEntity = response.getEntity(BookEntity.class);

        assertEquals(200, response.getStatus());
        assertEquals("LOTR", responseEntity.getTitle());
        assertEquals("J.K.Rowling", responseEntity.getAuthor());
        assertEquals(12.99, responseEntity.getPrice(), 0.0001);

    }
}

Also you can find the whole implementation from here

https://github.com/flexelem/jersey-client-example

I will appreciate for every response. Thanks anyway

quartaela
  • 2,579
  • 16
  • 63
  • 99

0 Answers0