Commit 053abe72 by dblevins

Starter project for TomEE JAX-RS with Arquillian Tests

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	new file:   pom.xml
#	new file:   src/main/java/org/superbiz/ColorBean.java
#	new file:   src/test/java/org/superbiz/ColorBeanTest.java
#	new file:   src/test/resources/arquillian.xml
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   README.md
#	modified:   src/main/java/org/superbiz/ColorBean.java
#	modified:   src/test/java/org/superbiz/ColorBeanTest.java
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	.idea/
#	target/
#	tomee-rest-arquillian.iml
parent 0018f9cd
tomee-jaxrs-starter-project # Apache TomEE JAX-RS Starter Project
===========================
What is better than a Maven Archetype? A repo already setup to go, that's what!
Just fork and start coding with this simple starter project. Includes:
- Simple `OrangeBean` JAX-RS service
- Basic `OrangeBeanTest` using Arquillian and the CXF `WebClient`
Starter project for TomEE JAX-RS w/ Arquillian Test. Fork-and-code
...@@ -37,6 +37,12 @@ public class ColorBean { ...@@ -37,6 +37,12 @@ public class ColorBean {
return color; return color;
} }
@Path("favorite")
@GET
public String getFavoriteColor() {
return "orange";
}
@Path("{color}") @Path("{color}")
@POST @POST
public void setColor(@PathParam("color") String color) { public void setColor(@PathParam("color") String color) {
......
...@@ -32,14 +32,43 @@ import java.io.IOException; ...@@ -32,14 +32,43 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
/**
* Arquillian will start the container, deploy all @Deployment bundles, then run all the @Test methods.
*
* A strong value-add for Arquillian is that the test is abstracted from the server.
* It is possible to rerun the same test against multiple adapters or server configurations.
*
* A second value-add is it is possible to build WebArchives that are slim and trim and therefore
* isolate the functionality being tested. This also makes it easier to swap out one implementation
* of a class for another allowing for easy mocking.
*
*/
@RunWith(Arquillian.class) @RunWith(Arquillian.class)
public class ColorBeanTest extends Assert { public class ColorBeanTest extends Assert {
/**
* ShrinkWrap is used to create a war file on the fly.
*
* The API is quite expressive and can build any possible
* flavor of war file. It can quite easily return a rebuilt
* war file as well.
*
* More than one @Deployment method is allowed.
*/
@Deployment @Deployment
public static WebArchive createDeployment() { public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class).addClass(ColorBean.class); return ShrinkWrap.create(WebArchive.class).addClass(ColorBean.class);
} }
/**
* This URL will contain the following URL data
*
* - http://<host>:<port>/<webapp>/
*
* This allows the test itself to be agnostic of server information or even
* the name of the webapp
*
*/
@ArquillianResource @ArquillianResource
private URL webappUrl; private URL webappUrl;
...@@ -47,10 +76,9 @@ public class ColorBeanTest extends Assert { ...@@ -47,10 +76,9 @@ public class ColorBeanTest extends Assert {
@Test @Test
public void postAndGet() throws Exception { public void postAndGet() throws Exception {
final WebClient webClient = WebClient.create(webappUrl.toURI());
// POST // POST
{ {
final WebClient webClient = WebClient.create(webappUrl.toURI());
final Response response = webClient.path("color/green").post(null); final Response response = webClient.path("color/green").post(null);
assertEquals(204, response.getStatus()); assertEquals(204, response.getStatus());
...@@ -58,6 +86,7 @@ public class ColorBeanTest extends Assert { ...@@ -58,6 +86,7 @@ public class ColorBeanTest extends Assert {
// GET // GET
{ {
final WebClient webClient = WebClient.create(webappUrl.toURI());
final Response response = webClient.path("color").get(); final Response response = webClient.path("color").get();
assertEquals(200, response.getStatus()); assertEquals(200, response.getStatus());
...@@ -69,13 +98,21 @@ public class ColorBeanTest extends Assert { ...@@ -69,13 +98,21 @@ public class ColorBeanTest extends Assert {
} }
@Test
public void getFavorite() throws Exception {
final WebClient webClient = WebClient.create(webappUrl.toURI());
final Response response = webClient.path("color/favorite").get();
assertEquals(200, response.getStatus());
final String content = slurp((InputStream) response.getEntity());
assertEquals("orange", content);
}
/** /**
* Reusable utility method * Reusable utility method
* Move to a shared class or replace with equivalent * Move to a shared class or replace with equivalent
*
* @param in
* @return
* @throws IOException
*/ */
public static String slurp(final InputStream in) throws IOException { public static String slurp(final InputStream in) throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream(); final ByteArrayOutputStream out = new ByteArrayOutputStream();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment