testing fix

This commit is contained in:
eugenp 2017-02-09 12:15:55 +02:00
parent 3a1505ad29
commit 22241a4126
1 changed files with 29 additions and 32 deletions

View File

@ -1,17 +1,12 @@
package com.baeldung.annotation.servletcomponentscan; package com.baeldung.annotation.servletcomponentscan;
import com.baeldung.annotation.servletcomponentscan.javaee.AttrListener; import static org.junit.Assert.assertEquals;
import com.baeldung.annotation.servletcomponentscan.javaee.EchoServlet; import static org.junit.Assert.assertNotNull;
import com.baeldung.annotation.servletcomponentscan.javaee.HelloFilter; import static org.junit.Assert.assertTrue;
import com.baeldung.annotation.servletcomponentscan.javaee.HelloServlet;
import org.jboss.arquillian.container.test.api.Deployment; import java.net.MalformedURLException;
import org.jboss.arquillian.container.test.api.RunAsClient; import java.net.URI;
import org.jboss.arquillian.junit.Arquillian; import java.net.URL;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;
import javax.inject.Inject; import javax.inject.Inject;
import javax.servlet.FilterRegistration; import javax.servlet.FilterRegistration;
@ -22,24 +17,31 @@ import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget; import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import static org.junit.Assert.*; import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.baeldung.annotation.servletcomponentscan.javaee.AttrListener;
import com.baeldung.annotation.servletcomponentscan.javaee.EchoServlet;
import com.baeldung.annotation.servletcomponentscan.javaee.HelloFilter;
import com.baeldung.annotation.servletcomponentscan.javaee.HelloServlet;
@RunWith(Arquillian.class) @RunWith(Arquillian.class)
public class JavaEEAppTest { public class JavaEEAppIntegrationTest {
@Deployment @Deployment
public static WebArchive createDeployment() { public static WebArchive createDeployment() {
return ShrinkWrap return ShrinkWrap.create(WebArchive.class).addClass(JavaEEApp.class).addClasses(AttrListener.class, HelloFilter.class, HelloServlet.class, EchoServlet.class);
.create(WebArchive.class)
.addClass(JavaEEApp.class)
.addClasses(AttrListener.class, HelloFilter.class, HelloServlet.class, EchoServlet.class);
} }
@Inject private ServletContext servletContext; @Inject
private ServletContext servletContext;
@Test @Test
public void givenServletContextListener_whenAccessSpecialAttrs_thenFound() throws MalformedURLException { public void givenServletContextListener_whenAccessSpecialAttrs_thenFound() throws MalformedURLException {
@ -54,21 +56,18 @@ public class JavaEEAppTest {
FilterRegistration filterRegistration = servletContext.getFilterRegistration("hello filter"); FilterRegistration filterRegistration = servletContext.getFilterRegistration("hello filter");
assertNotNull(filterRegistration); assertNotNull(filterRegistration);
assertTrue(filterRegistration assertTrue(filterRegistration.getServletNameMappings().contains("echo servlet"));
.getServletNameMappings()
.contains("echo servlet"));
} }
@ArquillianResource private URL base; @ArquillianResource
private URL base;
@Test @Test
@RunAsClient @RunAsClient
public void givenFilterAndServlet_whenGetHello_thenRespondFilteringHello() throws MalformedURLException { public void givenFilterAndServlet_whenGetHello_thenRespondFilteringHello() throws MalformedURLException {
Client client = ClientBuilder.newClient(); Client client = ClientBuilder.newClient();
WebTarget target = client.target(URI.create(new URL(base, "hello").toExternalForm())); WebTarget target = client.target(URI.create(new URL(base, "hello").toExternalForm()));
Response response = target Response response = target.request().get();
.request()
.get();
assertEquals("filtering hello", response.readEntity(String.class)); assertEquals("filtering hello", response.readEntity(String.class));
} }
@ -78,9 +77,7 @@ public class JavaEEAppTest {
public void givenFilterAndServlet_whenPostEcho_thenEchoFiltered() throws MalformedURLException { public void givenFilterAndServlet_whenPostEcho_thenEchoFiltered() throws MalformedURLException {
Client client = ClientBuilder.newClient(); Client client = ClientBuilder.newClient();
WebTarget target = client.target(URI.create(new URL(base, "echo").toExternalForm())); WebTarget target = client.target(URI.create(new URL(base, "echo").toExternalForm()));
Response response = target Response response = target.request().post(Entity.entity("echo", MediaType.TEXT_PLAIN_TYPE));
.request()
.post(Entity.entity("echo", MediaType.TEXT_PLAIN_TYPE));
assertEquals("filtering echo", response.readEntity(String.class)); assertEquals("filtering echo", response.readEntity(String.class));
} }