O'Reilly logo

RESTful Java with JAX-RS by Bill Burke

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Examining the Source Code

The server-side source code is exactly as posted in Chapter 3. What we haven’t already gone over is the client code for this example. The client code is structured as a JUnit class. JUnit is an open source Java library for defining unit tests. Maven automatically knows how to find JUnit-enabled test code and run it with the build. It scans the classes within the src/test/java directory, looking for classes that have methods annotated with @org.junit.Test. This example has only one: com.restfully.shop.test.CustomerResourceTest. Let’s go over the code for it:

src/test/java/com/restfully.shop.test.CustomerResourceTest.java

package com.restfully.shop.test;

import org.junit.Assert;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class CustomerResourceTest {
   @Test
   public void testCustomerResource() throws Exception  {

Our test class has only one method: testCustomerResource(). It is annotated with @Test. This tells Maven that this is a JUnit test:

 System.out.println("*** Create a new Customer ***"); // Create a new customer String newCustomer = "<customer>" + "<first-name>Bill</first-name>" + "<last-name>Burke</last-name>" + "<street>256 Clarendon Street</street>" + "<city>Boston</city>" + "<state>MA</state>" + "<zip>02115</zip>" + "<country>USA</country>" + "</customer>"; URL postUrl = new URL("http://localhost:9095/customers"); HttpURLConnection ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required