Automating the test cases

We start the server programmatically. For this, we have decided to use a Grizzly (https://javaee.github.io/grizzly/), which allows us to start the server using the configuration from Jersey's ResourceConfig (FQCN: org.glassfish.jersey.server.ResourceConfig), as shown in the test BooksEndpointTest (fragment).

The code can be found at https://bitbucket.org/vfarcic/tdd-java-alexandria:

public class BooksEndpointTest { 
    public static final URI FULL_PATH =  
      URI.create("http://localhost:8080/alexandria"); 
    private HttpServer server; 
 
    @Before 
    public void setUp() throws IOException { 
        ResourceConfig resourceConfig = 
          new MyApplication(); 
        server = GrizzlyHttpServerFactory 
          .createHttpServer(FULL_PATH, resourceConfig); 
        server.start(); 
    } 
 
    @After 
    public void tearDown(){ 
        server.shutdownNow(); 
    } 

This prepares a local server at the address http://localhost:8080/alexandria. It will only be available for a short period of time (while the tests run), so, if you need to manually access the server, whenever you want to pause the execution, insert a call to the following method:

public void pauseTheServer() throws Exception { 
    System.in.read(); 
} 

When you want to stop the server, stop the execution or hit Enter in the allocated console.

Now we can start the server programmatically, pause it (with the preceding method), and execute the spike again. The results are the same, so the refactor is successful.

We add the first automated test to the system.

The code can be found at https://bitbucket.org/vfarcic/tdd-java-alexandria:

public class BooksEndpointTest { 
 
   public static final String AUTHOR_BOOK_1 = 
"Viktor Farcic and Alex Garcia"; public static final String TITLE_BOOK_1 = "TDD in Java"; private final Map<String, String> TDD_IN_JAVA; public BooksEndpointTest() { TDD_IN_JAVA = getBookProperties(TITLE_BOOK_1,
AUTHOR_BOOK_1); } private Map<String, String> getBookProperties (String title, String author) { Map<String, String> bookProperties = new HashMap<>(); bookProperties.put("title", title); bookProperties.put("author", author); return bookProperties; } @Test public void add_one_book() throws IOException { final Response books1 = addBook(TDD_IN_JAVA); assertBooksSize(books1, is("1")); } private void assertBooksSize(Response response,
Matcher<String> matcher) { response.then().body(matcher); } private Response addBook (Map<String, ?> bookProperties) { return RestAssured .given().log().path() .contentType(ContentType.URLENC) .parameters(bookProperties) .post("books"); }

For testing purposes, we're using a library called RestAssured (https://github.com/rest-assured/rest-assured) that allows for easier testing of REST and JSON.

To complete the automated test suite, we create these tests:

  • add_one_book()
  • add_a_second_book()
  • get_book_details_by_id()
  • get_several_books_in_a_row()
  • censor_a_book()
  • cannot_retrieve_a_censored_book()

The code can be found at https://bitbucket.org/vfarcic/tdd-java-alexandria/.

Now that we have a suite that ensures that no regression is introduced, we take a look at the following to-do list:

  1. Dependency injection in BooksEndpoint for books
  2. Change status for statuses
  3. Remove the primitive obsession with status (optional)

We will tackle dependency injection first.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.17.18