How to do it...

  1. The first thing that we need to do is add the necessary dependencies for the Cucumber libraries to our build.gradle file, as follows:
dependencies { 
    compile("org.springframework.boot:spring-boot-starter-data-jpa") 
    compile("org.springframework.boot:spring-boot-starter-jdbc") 
    compile("org.springframework.boot:spring-boot-starter-web") 
    compile("org.springframework.boot:spring-boot-starter-data-rest") 
    compile project(":db-count-starter") 
    runtime("com.h2database:h2") 
    runtime("mysql:mysql-connector-java") 
    testCompile("org.springframework.boot:spring-boot-starter-test") 
    testCompile("info.cukes:cucumber-spring:1.2.5") 
    testCompile("info.cukes:cucumber-java8:1.2.5") 
    testCompile("info.cukes:cucumber-junit:1.2.5") 
} 
  1. Next, we will need to create a test driver class to run Cucumber tests. Let's create a RunCukeTests.java file in the src/test/java/com/example/bookpub directory at the root of our project with the following content:
@RunWith(Cucumber.class) 
@CucumberOptions(plugin={"pretty", "html:build/reports/cucumber"},   
                 glue = {"cucumber.api.spring",           
"classpath:com.example.bookpub"}, monochrome = true) public class RunCukeTests { }
  1. With the driver class created, we are ready to start writing what Cucumber refers to as Step Definitions. I will talk briefly about what these are in the How it works... section of this recipe. For now, let's create a RepositoryStepdefs.java file in the src/test/java/com/example/bookpub directory at the root of our project with the following content:
@WebAppConfiguration 
@ContextConfiguration(classes = BookPubApplication.class, 
loader = SpringBootContextLoader.class) public class RepositoryStepdefs { @Autowired private WebApplicationContext context; @Autowired private DataSource ds; @Autowired private BookRepository bookRepository; private Book loadedBook; @Given("^([^"]*) fixture is loaded$") public void data_fixture_is_loaded(String fixtureName)
throws Throwable { ResourceDatabasePopulator populator
= new ResourceDatabasePopulator
(context.getResource("classpath:/" + fixtureName + ".sql")); DatabasePopulatorUtils.execute(populator, ds); } @Given("^(d+) books available in the catalogue$") public void books_available_in_the_catalogue(int bookCount)
throws Throwable { assertEquals(bookCount, bookRepository.count()); } @When("^searching for book by isbn ([d-]+)$") public void searching_for_book_by_isbn(String isbn)
throws Throwable { loadedBook = bookRepository.findBookByIsbn(isbn); assertNotNull(loadedBook); assertEquals(isbn, loadedBook.getIsbn()); } @Then("^book title will be ([^"]*)$") public void book_title_will_be(String bookTitle)
throws Throwable { assertNotNull(loadedBook); assertEquals(bookTitle, loadedBook.getTitle()); } }
  1. Now, we will need to create a corresponding testing feature definition file named repositories.feature in the src/test/resources/com/example/bookpub directory at the root of our project with the following content:
@txn 
Feature: Finding a book by ISBN 
  Background: Preload DB Mock Data 
    Given packt-books fixture is loaded 
 
  Scenario: Load one book 
    Given 3 books available in the catalogue 
    When searching for book by isbn 978-1-78398-478-7 
    Then book title will be Orchestrating Docker 
  1. Lastly, we will create one more data SQL file named packt-books.sql in the src/test/resources directory at the root of our project with the following content:
INSERT INTO author (id, first_name, last_name) VALUES (5, 'Shrikrishna', 'Holla') 
INSERT INTO book (isbn, title, author_id, publisher_id) VALUES ('978-1-78398-478-7', 'Orchestrating Docker', 5, 1) 
  1. Execute the tests by running ./gradlew clean test and the tests should get passed.
  1. With the addition of Cucumber, we also get the results of the tests in both the JUnit report and Cucumber-specific report HTML files. If we open build/reports/tests/index.html in the browser and click on the Classes button, we will see our scenario in the table, as shown in the following screenshot:
  2. Selecting the Scenario: Load one book link will take us to the detailed report page, as shown in the following screenshot:
  1. As we can see, the descriptions are nicer than the class and method names that we saw in the original JUnit-based test cases.
  2. Cucumber also generates its own report, which can be viewed by opening build/reports/cucumber/index.html in the browser.
  3. Being a behavior-driven testing framework, the feature files allow us not only to define individual conditions, but also to declare entire scenario outlines, which make the defining of multiple assertions of similar data easier. Let's create another feature file named restful.feature in the src/test/resources/com/example/bookpub directory at the root of our project with the following content:
@txn 
Feature: Finding a book via REST API 
  Background: 
    Given packt-books fixture is loaded 
 
  Scenario Outline: Using RESTful API to lookup books by ISBN 
    Given catalogue with books 
    When requesting url /books/<isbn> 
    Then status code will be 200 
    And response content contains <title> 
 
    Examples: 
      |isbn             |title               | 
      |978-1-78398-478-7|Orchestrating Docker| 
      |978-1-78528-415-1|Spring Boot Recipes | 
  1. We will also create a corresponding RestfulStepdefs.java file in the src/test/java/com/example/bookpub directory at the root of our project with the following content:
import cucumber.api.java.Before; 
import cucumber.api.java.en.Given; 
import cucumber.api.java.en.Then; 
import cucumber.api.java.en.When; 
 
import static org.hamcrest.CoreMatchers.containsString; 
import static org.junit.Assert.assertTrue; 
import static org.junit.Assert.assertNotNull; 
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; 
 
@WebAppConfiguration 
@ContextConfiguration(classes = BookPubApplication.class, loader = SpringBootContextLoader.class)  
public class RestfulStepdefs { 
  @Autowired 
  private WebApplicationContext context; 
  @Autowired 
  private BookRepository bookRepository; 
 
  private MockMvc mockMvc; 
  private ResultActions result; 
 
  @Before 
  public void setup() throws IOException { 
    mockMvc = 
MockMvcBuilders.webAppContextSetup(context).build(); } @Given("^catalogue with books$") public void catalogue_with_books() { assertTrue(bookRepository.count() > 0); } @When("^requesting url ([^"]*)$") public void requesting_url(String url) throws Exception { result = mockMvc.perform(get(url)); } @Then("^status code will be ([d]*)$") public void status_code_will_be(int code) throws
Throwable { assertNotNull(result); result.andExpect(status().is(code)); } @Then("^response content contains ([^"]*)$") public void response_content_contains(String content)
throws Throwable { assertNotNull(result); result.andExpect( content().string(containsString(content)) ); } }
  1. Execute the tests by running ./gradlew clean test and the tests should continue to get passed.
..................Content has been hidden....................

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