Techies.

Custom Tests with SpringBot

This guide will demonstrate adding custom tests to a SpringBot project.


The current version of SpringBot doesn’t build the test framework on the platform. The next version of SpringBot will resolve this issue.

Preface

By default, the bots write tests for the client-side, server-side and test target.

  • Client-side: These tests reside in clientside in form of *.spec.ts files. Angular uses the Jasmine test framework by default.
  • Server-side: These tests reside in serverside. Spring uses JUnit bundled with its
    test framework for unit and integration tests.
  • Test target: Unlike the server-side tests, these tests are written by a separate Java test target which focuses on end-to-end or user acceptance testing with Selenium and Cucumber.

In this guide, we will add some small custom tests for the test target within the Codebots Zoo project.

Specifically, let’s add a custom test to make sure that our application does not accept login from an unregistered account.

We will be required to interact with the following file:

File Name Purpose
Login.feature Cucumber file where we will add a custom scenario to test unregistered account login
ThenStepDef.java We use this file to define a custom step definition for our custom test

Login.feature

Feature files are Cucumber files that contain multiple scenarios. Each scenario depicts multiple steps in form of “Given”, “When” and “Then”. For example:

Scenario: My name
    Given I am called "John Smith"
    When Someone says "John Smith"
    Then I say "Yes?"

Combined with Selenium, this allows us to construct and run simple and complex scenarios that mimic user interaction with our application. For this example, we will be modifying login.feature to add our new custom scenario.

This file can be located at testtarget/src/test/java/feature/botwritten/login/Login.feature.

  1. Find the additional scenarios protected region: Add any additional scenarios here
  2. Turn it on by replacing off begin to on begin
  3. Add the following code snippet into the body of the protected region between that line and the one below it:
@loud-login
@unsuccessful
@now
Scenario: Custom test
    Given I login with the username notreal@example.com and the password notreal
    Then I confirm that I am on a sub route of the base client called login
    And There is an unsuccessful alert message
    And I want to print a simple message "notreal@example.com cannot be logged in" to the console

In this custom scenario, we want to attempt to log in with a non-existing account, and expect an alert message to be displayed. We also want to print a message to the console when that happens.

This test has also been annotated with @now. This allows us to run this specific test in the next section.

ThenStepDef.java

This file can be modified to add a new custom step definition that will be called from our custom scenario. It is located at testtarget/src/test/java/stepdef/botwritten/ThenStepDef.java.

Follow these steps to add a custom step definition:

  1. Locate the additional class methods protected region: // % protected region % [Add any additional class methods here] off begin
  2. Turn the protected region on by replacing off begin with on begin
  3. Insert the following code block below the line:

    /**
    
  • Print a simple message to the console.
    /
    @Then(“^I want to print a simple message \”([^\”]
    )\” to the console$”)
    public void I_want_to_print_a_simple_message_to_the_console(String message) {
    System.out.println(message);
    }

        
    

Running Test

In order to run the specific test, we need to inform Cucumber that we want to run all the scenarios that have the tag @now. As there is only one scenario with that tag, we can safely assume that only our scenario will be run.

You do not need to modify any file as parametising tests is built into the test framework. These tests can be run via either the command line or your IDE.

This article we will walkthrough both the command line and IntelliJ IDEA.

You will need to start up your server and client before running any Cucumber tests.

Command line

  1. Open up your command line terminal and navigate to your testtarget directory.
  2. Run ./gradlew cleanTest test -Pcucumber.options="--tags @now". By running this command, we are running all the tests in the test target that are annotated with @now tag.
  3. Once done, results will be displayed on the console. Aside from the console output, you can also find test results in a HTML format in testtarget/src/test/resources/output.

Image

IntelliJ IDEA

  1. In the IntelliJ window, select Run -> Edit Configurations from the menu bar. You should now be able to see the following window:
    img
  2. Click the + icon on the top left corner and then select Gradle. You should now see a new configuration for running Gradle tasks.
    img
  3. In Configuration tab, on the first line Gradle project, click the folder icon on the right of the text field and select <your project name>--test.

    If the folder icon does not list your project, you might have not imported the Gradle project into IntelliJ correctly. Click the ... icon on the right of the folder icon, then navigate and select your testtarget directory.

  4. Type in test in Tasks text field. If your project has been imported correctly, IntelliJ will offer proper code completion for the task.
  5. Type in -Pcucumber.options="--tags @blah" in the Arguments text field.
  6. Click OK at the bottom right. Your run configuration should now look like this:
    img
  7. IntelliJ will auto apply and select the latest run configuration, which is the one you just created. Click on the green arrow or Run -> 'Run <your run configuration name>' to run the test.
  8. Once the tests are done, you can find the reports at testtarget/src/test/resources/output.

Image

Last updated: 16 December 2019


Related articles