Run the Cucumber test specification

Collaborate on cutting-edge hong kong data technologies and solutions.
Post Reply
suchona.kani.z
Posts: 194
Joined: Sat Dec 21, 2024 5:52 am

Run the Cucumber test specification

Post by suchona.kani.z »

Add and configure the Cucumber-Maven dependenc To run the test specification, we need some dependencies in the Now we can add the acceptance test we defined above to our code base in src/test/resources/features/collect-thought.feature

To make Maven execute this specification, we need some boilerplate code.

First, we need a test class that references the Cucumber test specifications:



src/test/java/de/adesso/thalheim/gtd/CucumberTest.java

We also need to provide Cucumber context.

src/test/java/de/adesso/thalheim/gtd/CucumberSpringBootDemoApplicationTest.java

Make sure that the RANDOM port does not interfere with your regularly running local instance of this service.

We now run Maven. During the test run, we get an error message saying that the glue code is missing, so let's add it:



Our test specification now fails. But unfortunately it kuwait consumer email list doesn't fail for the correct reason! So we implemented the glue code in src/test/java/de/adesso/thalheim/gtd/CaptureStepDefinitions.java :




The test defines that we POSTneed an endpoint that gtd/thoughtsis output in the context path. This should return HTTP status code 200.

I also added the AssertJ Core library to the Maven dependencies. assertThat(...)...Sounds more like BDD than the standard JUnit assert statements.

Now when running the Cucumber tests or the Maven build, the test execution fails because no REST controller offers a correct endpoint. Now we have a test that fails for the correct reason:


[ERROR] Collect Thought Time elapsed: 0.248 s <<< ERROR!
org.apache.http.conn.HttpHostConnectException: Connect to localhost:8080 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
Caused by: java.net.ConnectException: Connection refused: connect

The reason our test fails: There is no REST endpoint listening where we expect it.

This means that we can now finally write the production code:



src/main/java/de/adesso/thalheim/gtd/controller/InboxController.java

The acceptance test fails again because there is no glue code for the When clause in the Cucumber scenario. Therefore, we write the glue code in the src/test/java/de/adesso/thalheim/gtd/CaptureStepDefinitions.java :


Note on the level of abstraction
Here you can see that I placed the glue code and thus the acceptance test on a higher level of abstraction, above the concrete interface. Of course, I could have inserted the REST controller with @Inject and used simple Java for the tests. That would have made things a lot easier. But that would have made the test more concrete than necessary and the test would also have been tied to implementation details.

Now we can write a method for the GET endpoint. It should return a list of classes that contains exactly one field "description". We need to implement the controller. So let's write that first in normal TDD style with a test case.
Post Reply