Skip to content

Test framework that uses Spring, Serenity-Cucumber, Java 17, JUnit 5 and SLF4J to provide a basic harness. SerenityRest and Serenity Webdriver test examples provided

License

Notifications You must be signed in to change notification settings

cmccarthyIrl/spring-cucumber-serenity-test-harness

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Cucumber Serenity Test Harness

Index

Start | Maven | Quickstart |
Run | Command Line | IDE Support | Java JDK | Troubleshooting |
Report | Configuration | Environment Switching | Serenity Reports | Logging |
Advanced | Before / After Hooks | JSON Transforms | Contributing |

Maven

The Framework uses Spring Boot Test , Cucumber and Serenity client implementations.

Spring <dependencies>:

<dependecies>
    ...
    <dependency>
        <groupId>org.springframework.amqp</groupId>
        <artifactId>spring-rabbit</artifactId>
        <version>${spring-rabbit.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
    </dependency>
    ...
</dependecies>

Serenity <dependencies>:

<dependecies>
    ...
    <dependency>
      <groupId>net.serenity-bdd</groupId>
      <artifactId>serenity-core</artifactId>
      <version>${serenity.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>net.serenity-bdd</groupId>
      <artifactId>serenity-junit</artifactId>
      <version>${serenity.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>net.serenity-bdd</groupId>
      <artifactId>serenity-screenplay</artifactId>
      <version>${serenity.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>net.serenity-bdd</groupId>
      <artifactId>serenity-screenplay-webdriver</artifactId>
      <version>${serenity.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>net.serenity-bdd</groupId>
      <artifactId>serenity-ensure</artifactId>
      <version>${serenity.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>net.serenity-bdd</groupId>
      <artifactId>serenity-cucumber6</artifactId>
      <version>${serenity.cucumber.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>net.serenity-bdd</groupId>
      <artifactId>serenity-rest-assured</artifactId>
      <version>2.2.12</version>
    </dependency>
    <dependency>
      <groupId>net.serenity-bdd</groupId>
      <artifactId>serenity-spring</artifactId>
      <version>2.2.12</version>
    </dependency>
    ...
</dependecies>

Quickstart

JUnit

By using the JUnit Framework @RunWith Annotation Type we can utilize Serenity and Cucumber @CucumberOptions Annotation Type to execute the *.feature file tests

Right click the WeatherTest class and select Run

@RunWith(CucumberWithSerenity.class)
@CucumberOptions(
        plugin = {"pretty", "json:target/cucumber/report.json"},
        features = "src/test/resources/features/WeatherTest.feature",
        glue = {"com/cmccarthy/api", "com/cmccarthy/common"}
)
public class WeatherRunnerTest {}

Command Line

Normally you will use your IDE to run a *.feature file directly or via the *Test.java class. With the Test class, we can run tests from the command-line as well.

Note that the mvn test command only runs test classes that follow the *Test.java naming convention.

You can run a single test or a suite or tests like so :

mvn test -Dtest=WeatherTest
mvn test -Dtest=JunitSuiteTest

Note that the mvn clean install command runs all test Classes that follow the *Test.java naming convention

mvn clean install

IDE Support

To minimize the discrepancies between IDE versions and Locales the <sourceEncoding> is set to UTF-8

<properties>
    ...
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    ...
</properties>

Java JDK

The Java version to use is defined in the maven-compiler-plugin

<build>
    ...
    <pluginManagement>
        <plugins>
            ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
            ...
        </plugins>
    </pluginManagement>
    ...
</build>

Configuration

The AbstractTestDefinition class is responsible for specifying each Step class as @SpringBootTest and its @ContextConfiguration

All the Step Classes in the Framework should extend the AbstractTestDefinition class

@ContextConfiguration(classes = {FrameworkContextConfiguration.class})
@SpringBootTest
public class AbstractTestDefinition {
}

The FrameworkContextConfiguration class is responsible for specifying the Spring @Configuration, modules to scan, properties to use etc

@EnableRetry
@Configuration
@ComponentScan({
        "com.cmccarthy"
})
@PropertySource("application.properties")
public class FrameworkContextConfiguration {
}

Environment Switching

There is only one thing you need to do to switch the environment - which is to set <activeByDefault> property in the Master POM.

By default, the value of spring.profiles.active is defined in the application.properties file which inherits its value from the Master POM property <activeByDefault>

<profiles>
    ...
    <profile>
        <id>prod</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <activatedProperties>prod</activatedProperties>
        </properties>
    </profile>
    ...
</profiles>

You can then specify the profile to use when running Maven from the command line like so:

mvn clean install -P dev

Below is an example of the application.properties file.

spring.profiles.active=@activatedProperties@

Serenity Reports

The Framework uses Serenity Reports to generate the HTML Test Reports

To generate the Serenity Reports execute the following from the command line

mvn serenity:aggregate

The example below is a report generated by Serenity Reports open-source library.

Logging

The Framework uses Log4j2 You can instantiate the logging service in any Class like so

        private final Logger logger=LoggerFactory.getLogger(WikipediaPageSteps.class);

you can then use the logger like so :

        logger.info("This is a info message");
        logger.warn("This is a warning message");
        logger.debug("This is a info message");
        logger.error("This is a error message");

Before / After Hooks

The Log4j2 logging service is initialized from the Hooks.class

public class Hooks {

    @Autowired
    private HookUtils hookUtil;

    @After
    public void afterScenario(Scenario scenario) {
        hookUtil.endOfTest(scenario);
    }
}

JSON Transforms

Rest Assured IO is used to map the Response Objects to their respective POJO Classes

<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>3.0.0</version>
</dependency>

Troubleshooting

  • Execute the following commands to resolve any dependency issues
    1. cd ~/spring-cucumber-serenity-test-harness
    2. mvn clean install -DskipTests

Contributing

Spotted a mistake? Questions? Suggestions?

Open an Issue

About

Test framework that uses Spring, Serenity-Cucumber, Java 17, JUnit 5 and SLF4J to provide a basic harness. SerenityRest and Serenity Webdriver test examples provided

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published