Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Our simple microservice is now setupset up, that's all there is to it.  We want to use CWB REST to test our /books endpoint, to be able to do that we first need to setup the project.  This involves some fairly straightforward steps that only need to happen once.

...

Adding a feature and JUnit test

Having setup set up the basic CWB configuration, we can add a unit test class that should execute Cucumber features, and add a basic feature file.

...

To be able to start the webserver from the unit test we only need to call the CwbRestApplication main method from out our unit test.  We can easily hook this up through the cucumber.xml file.  The cucumber.xml contains the Spring ApplicationContext configuration for running the CWB features.  This ApplicationContext is started a single time before all features are executed, this makes it an excellent location to add a call to the CwbRestApplication.

...

Code Block
languagexml
titleXML bean configuration to add to cucumber.xml
<bean id="startTomcat" class="org.springframework.beans.factory.config.MethodInvokingBean">
   <property name="staticMethod" value="com.foreach.cwb.tutorials.CwbRestApplication.main" />
</bean>

At this point you can shutdown shut down any running application instance you might still have.  If you now execute TestWebRestFeatures you will see the Spring Boot banner show up in your output, meaning the application and embedded Tomcat are started before feature execution.  If you configured everything the right way, simply executing the unit test should yield a green result.

...

  • an embedded Tomcat is started on port 8080
  • the Spring Boot microservice application is loaded (and the books are created)
  • a call to http://localhost:8080/books is being made by CWB REST code
  • books are feched fetched from the H2 database and returned to the caller

We're executing the full application stack... but ofcourse of course we are not testing very much.

...

Without having to change any of the features, we now startup start up the application on a random port and connect to it.

...

Code Block
languagetext
titleScenario for creating a book
Scenario: A newly created book should be added to the list
  Given i call post "/books" with data
    """
      title: "My book: #{id.scenario}"
      author: Myself
    """
  And the response status is 201
  When i call get "/books"
  Then the response entity "_embedded.books[]" should contain:
    | title  | My book: #{id.scenario} |
    | author | Myself                  |

This scenario show cases showcases some other features of CWB REST: posting data using YAML and using a table syntax to find an item in an array.

...