Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edenir-anschau/0f7dba1b72e901a340dc486a77b20fbf to your computer and use it in GitHub Desktop.
Save edenir-anschau/0f7dba1b72e901a340dc486a77b20fbf to your computer and use it in GitHub Desktop.
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