JUnit de services Web Spring3 / REST avec un serveur Tomcat 7 embarqué
Bonjour,
je cherche a tester des services Web / REST d'une application Spring MVC 3.0.5 (sous Maven) via des jUnit en utilisant un serveur Tomcat 7.0.22 embarqué.
Je n'arrive pas a trouver la manière de configurer le serveur pour qu'il demarre l'application Spring correctement, j'ai le message d'erreur laconique suivant:
"Failed to start component [StandardServer[-1]]"
Est ce que quelqu'un peut m'aider ?? Par avance merci !!
le projet a une structure webApp Maven standard:
Code:
1 2 3 4 5 6 7 8 9
|
src
+-main
+-java
+-webapp
+-static
+-WEB-INF
+-web.xml
+-applicationcontext.xml |
j'ai une classe abstraite de test gérant le serveur:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
import java.io.File;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.apache.catalina.Host;
import org.apache.catalina.LifecycleState;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.ContextConfig;
import org.apache.catalina.startup.Tomcat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "application-servlet.xml", "applicationContext.xml", "securityContext.xml" })
public abstract class AbstractMainHTTPTestCase {
private static final Logger LOG = LoggerFactory.getLogger(AbstractMainHTTPTestCase.class);
private static String hostname = "localhost";
private static int PORT = 8081;
private static String protocol = "org.apache.coyote.http11.Http11Protocol";
private static String contextPath = "/MyApp";
private Tomcat tomcat;
private String baseDir = new java.io.File("").getAbsolutePath();
private String appPath;
private String WebXmlPath;
public Client client;
public WebResource webResource;
public String response;
@Before
public final void setUp() throws Exception {
this.startHTTPServer();
}
public final void startHTTPServer() throws Exception {
// Create temporary folders
appPath = new java.io.File(this.baseDir + "/src/main/webapp").getAbsolutePath();
WebXmlPath = new java.io.File(this.appPath + "/WEB-INF/web.xml").getAbsolutePath();
File tempDir = new File(this.baseDir + "/target", "webtest");
if (!tempDir.exists() && !tempDir.mkdirs()) {
LOG.error("Unable to create webtest TEMP directory for test:{}", tempDir.getAbsolutePath());
}
File appBase = new File(this.baseDir + "/target/webtest/", "webapps");
if (!appBase.exists() && !appBase.mkdir()) {
LOG.error("Unable to create appBase for test:{}", appBase.getAbsolutePath());
}
// Log parameters values
LOG.debug("baseDir: {}", this.baseDir);
LOG.debug("appPath: {}", this.appPath);
LOG.debug("WebXmlPath: {}", this.WebXmlPath);
LOG.debug("tempDir: {}", tempDir.getAbsolutePath());
LOG.debug("appBase: {}", appBase.getAbsolutePath());
try {
this.tomcat = new Tomcat();
this.tomcat.setHostname(AbstractMainHTTPTestCase.hostname);
this.tomcat.setBaseDir(tempDir.getAbsolutePath());
Connector connector = new Connector(protocol);
connector.setPort(AbstractMainHTTPTestCase.PORT);
connector.setAttribute("connectionTimeout", "3000");
this.tomcat.getService().addConnector(connector);
this.tomcat.setConnector(connector);
this.tomcat.getHost().setAppBase(appBase.getAbsolutePath());
StandardContext ctxt = (StandardContext) tomcat.addContext(AbstractMainHTTPTestCase.contextPath, this.appPath);
ctxt.setDefaultWebXml(this.WebXmlPath);
ctxt.addLifecycleListener(new ContextConfig());
ctxt.setDocBase(tempDir.getAbsolutePath());
ctxt.setPath(AbstractMainHTTPTestCase.contextPath);
LOG.debug("Tomcat 7 server starting");
this.tomcat.start();
this.tomcat.getServer().await();
LOG.debug("Tomcat 7 server started (PORT: {})",
AbstractMainHTTPTestCase.PORT);
this.client = Client.create();
this.webResource = this.client.resource("http://localhost:" + AbstractMainHTTPTestCase.PORT);
} catch (Exception ex) {
LOG.error("Error while starting tomcat server");
this.tearDown();
}
}
@After
public final void tearDown() throws Exception {
if (tomcat.getServer() != null
&& tomcat.getServer().getState() != LifecycleState.DESTROYED) {
if (tomcat.getServer().getState() != LifecycleState.STOPPED) {
tomcat.stop();
}
tomcat.destroy();
}
}
public int getPORT() {
return AbstractMainHTTPTestCase.PORT;
}
} |
une classe de test:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sproutcore.sample.todos.AbstractMainHTTPTestCase;
public class MainControllerTest extends AbstractMainHTTPTestCase {
private static final Logger LOG = LoggerFactory.getLogger(MainControllerTest.class);
public MainControllerTest() {
super();
LOG.debug("HTTPUnit tests for: {}", this.getClass().getName());
}
@Test
public void staticTest() throws Exception {
LOG.debug("HTTP TEST: staticTest");
this.response = this.webResource.path("/static/test.txt").get(String.class);
LOG.debug("staticTest response: {}", this.response);
assertNotNull("Static test shall be not null", this.response);
@Test
public void getListTest() throws Exception
{
LOG.debug("HTTP TEST: getListTest");
this.response = this.webResource.path("/api/list").get(String.class);
assertNotNull("getListTest test shall be not null", this.response);
LOG.debug("getTaskTest response: {}", this.response);
}
} |
et une partie du fichier web.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
<!-- Reads request input using UTF-8 encoding -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Handles all requests into the application -->
<servlet>
<servlet-name>application</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>application</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> |
je pense que ça pourrait intéresser du monde... Quelqu'un a une idée ??