You are previewing RESTful Java with JAX-RS.

RESTful Java with JAX-RS

Cover of RESTful Java with JAX-RS by Bill Burke Published by O'Reilly Media, Inc.
O'Reilly logo

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 ...

The best content for your career. Discover unlimited learning on demand for around $1/day.