Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

JAX-RS Arquillian TestCase with @ArquillianResource injection

View CustomerResourceClientTest.java
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
@RunWith(Arquillian.class)
public class CustomerResourceClientTest
{
private static final String REST_PATH = "rest";
@Deployment(testable = false)
public static Archive<?> createDeployment()
{
return ShrinkWrap.create(WebArchive.class)
.addPackage(Customer.class.getPackage())
.addClasses(CustomerResource.class)
.addDirectory("WEB-INF/lib") // RESTEASY-507
.addWebResource("import.sql", "classes/import.sql")
.addWebResource(EmptyAsset.INSTANCE, "beans.xml")
.addManifestResource(new StringAsset(
Descriptors.create(PersistenceDescriptor.class)
.version("1.0")
.persistenceUnit("sellmore")
.provider(HIBERNATE)
.jtaDataSource("java:/DefaultDS")
.schemaGenerationMode(CREATE_DROP)
.showSql()
.exportAsString()
), "persistence.xml")
.setWebXML(new StringAsset(
Descriptors.create(WebAppDescriptor.class)
.version("3.0")
.contextParam("resteasy.scan", "true")
.contextParam("resteasy.servlet.mapping.prefix", REST_PATH)
.exportAsString()
));
}
 
@Test
public void testGetCustomerByIdUsingClientRequest(@ArquillianResource URL base) throws Exception
{
// GET http://localhost:8080/test/rest/customer/1
ClientRequest request = new ClientRequest(new URL(base, REST_PATH + "/customer/1").toExternalForm());
request.header("Accept", MediaType.APPLICATION_XML);
 
// we're expecting a String back
ClientResponse<String> responseObj = request.get(String.class);
 
Assert.assertEquals(200, responseObj.getStatus());
System.out.println("GET /customer/1 HTTP/1.1\n\n" + responseObj.getEntity());
Assert.assertEquals(
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
"<customer><id>1</id><name>Acme Corporation</name></customer>",
responseObj.getEntity());
}
}

Perfect, exactly what I wanted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.