Skip to content

Instantly share code, notes, and snippets.

@xdev-developer
Last active February 16, 2017 18:55
Embed
What would you like to do?
Run jetty in junit
package test;
import org.apache.http.HttpStatus;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.bio.SocketConnector;
import org.eclipse.jetty.webapp.WebAppContext;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static com.jayway.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
public class RestfullServiceTest {
Server server;
private Integer PORT = 777;
private String PROJECT_DIR = "web-service";
@Before
public void setUp() throws Exception {
server = new Server(PORT);
if(new File(PROJECT_DIR).exists()){
new WebAppContext(server, PROJECT_DIR +"/src/main/webapp/", "/" );
}else {
new WebAppContext(server, "src/main/webapp/", "/" );
}
SocketConnector connector = new SocketConnector();
connector.setMaxIdleTime(1000 * 60 * 60);
connector.setSoLingerTime(-1);
connector.setPort(0);
server.setConnectors(new Connector[]{connector});
server.setStopAtShutdown( true );
server.start();
PORT = connector.getLocalPort();
}
@After
public void tearDown() throws Exception {
server.stop();
}
private String getUrl(String url){
return "http://localhost:" + PORT + url;
}
@Test
public void testRest(){
given().expect()
.response().statusCode(HttpStatus.SC_OK)
.and().body(containsString("YES!"))
.when().get(getUrl("/alive"));
}
}
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>8.1.8.v20121106</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>test-jetty-servlet</artifactId>
<version>8.1.8.v20121106</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>8.1.8.v20121106</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>8.1.8.v20121106</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>8.1.8.v20121106</version>
<type>jar</type>
<scope>test</scope>
</dependency>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment