Skip to content

Commit

Permalink
#32: Add capability to write QuarkusIntegrationTests
Browse files Browse the repository at this point in the history
While at it, reformat and unify code.
  • Loading branch information
turing85 committed Jun 23, 2023
1 parent 15698f2 commit 08f51ae
Show file tree
Hide file tree
Showing 22 changed files with 500 additions and 363 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkiverse.cucumber.deployment;
package io.quarkiverse.cucumber;

import java.util.Arrays;
import java.util.HashSet;
Expand All @@ -8,16 +8,15 @@

import io.cucumber.java.StepDefinitionAnnotation;
import io.cucumber.java.StepDefinitionAnnotations;
import io.quarkiverse.cucumber.CucumberQuarkusTest;
import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.processor.DotNames;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.IndexDependencyBuildItem;

@SuppressWarnings("unused")
class CucumberProcessor {

private static final String FEATURE = "cucumber";

@BuildStep
Expand Down Expand Up @@ -53,6 +52,10 @@ AdditionalBeanBuildItem beanDefiningAnnotation(CombinedIndexBuildItem indexBuild
.getAllKnownSubclasses(DotName.createSimple(CucumberQuarkusTest.class.getName()))) {
stepClasses.add(i.name().toString());
}
for (var i : indexBuildItem.getIndex()
.getAllKnownSubclasses(DotName.createSimple(CucumberQuarkusIntegrationTest.class.getName()))) {
stepClasses.add(i.name().toString());
}
return AdditionalBeanBuildItem.builder().addBeanClasses(stepClasses).setDefaultScope(DotNames.SINGLETON)
.setUnremovable().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
import io.quarkiverse.cucumber.CucumberQuarkusTest;

public class BasicTest extends CucumberQuarkusTest {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

@Path("/")
public class Endpoint {

@GET
public String hello() {
return "hello";
}
}
}
13 changes: 7 additions & 6 deletions deployment/src/test/java/io/quarkiverse/cucumber/test/Steps.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,24 @@
import io.restassured.response.ValidatableResponse;

public class Steps {
private final String target;

@Inject
@ConfigProperty(defaultValue = "/")
String target;
public Steps(@ConfigProperty(name = "some.endpoint.path", defaultValue = "/") String target) {
this.target = target;
}

private ValidatableResponse result;

@Given("I call the endpoint")
public void i_call_endpoint() throws Exception {
public void i_call_endpoint() {
result = given()
.when().get(target)
.then();
}

@Then("the response is ok")
public void response_is_ok() throws Exception {
public void response_is_ok() {
result.statusCode(200);
}

}
}
39 changes: 37 additions & 2 deletions docs/modules/ROOT/pages/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,54 @@ In your `pom.xml` file, add:

== Usage

=== Quarkus Test setup

To bootstrap Cucumber add the following class to your test suite:

[source,java]
----
import io.quarkiverse.cucumber.CucumberQuarkusTest;
public class MyTest extends CucumberQuarkusTest {
}
----

This will automatically bootstrap Cucumber, and discover any `.feature` files and step classes that provide glue code.

=== Integration test setup

For integration tests to work, the `weld-junit5` extension is used and preconfigured. Aside from enabling integration test support, it also enabled basic injection support.

To run a cucumber test as integration test just extend `CucumberQuarkusIntegrationTest`:

[source,java]
----
import io.quarkiverse.cucumber.CucumberQuarkusIntegrationTest;
public class MyIT extends CucumberQuarkusIntegrationTest {
}
----

In case the integration test is located in a sibling-package of other bean-defining classes, we have to configure the packages to scan:

[source,java]
----
import io.quarkiverse.cucumber.CucumberIntegrationTest;
This will automatically bootstrap Cucumber, and discover any `.feature` files and step classes that provide glue code.
public class MyIT extends CucumberQuarkusIntegrationTest {
@Override
protected Class<?>[] packagesToScanRecursively() {
return new Class[] { SomeClassInASiblingPackage.class, SomeClassInAnotherSiblingPackage.class };
}
}
----

Weld will scan these packages recursively. This means that if a class is located in a package that is a parent of all other bean-defining packages, it is sufficient to reference this bean (see `CucumberOptionsIT` for an example).

As of now, the injection support has the following limitations:

* Only beans with `@ApplicationScoped` are detected
* classes defining the glue need to be annotated with `@ApplicationScoped`

== IDE Integration

Expand All @@ -50,7 +85,7 @@ You need to add the following `main` method to your test class:
import io.quarkiverse.cucumber.CucumberQuarkusTest;
public class MyTest extends CucumberQuarkusTest {
public static void main(String[] args) {
public static void main(String... args) {
runMain(MyTest.class, args);
}
}
Expand Down
25 changes: 6 additions & 19 deletions integration-tests/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.quarkiverse.cucumber</groupId>
Expand Down Expand Up @@ -42,6 +43,10 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${version.surefire.plugin}</version>
</plugin>
</plugins>
</build>
<profiles>
Expand All @@ -60,24 +65,6 @@
<skipTests>${native.surefire.skip}</skipTests>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.quarkiverse.cucumber.it;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/cucumber")
@ApplicationScoped
import org.eclipse.microprofile.config.inject.ConfigProperty;

@Path(CucumberResource.PATH)
@Produces(MediaType.TEXT_PLAIN)
public class CucumberResource {
public static final String PATH = "cucumber";

private final String greeting;

public CucumberResource(@ConfigProperty(name = "greeting") String greeting) {
this.greeting = greeting;
}

@GET
public String hello() {
return "Hello cucumber";
return greeting;
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
testPath=/cucumber
greeting="Hello, cucumber!"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.quarkiverse.cucumber.it;

import io.quarkiverse.cucumber.CucumberQuarkusIntegrationTest;

public class CucumberResourceIT extends CucumberQuarkusIntegrationTest {
public static void main(String... args) {
runMain(CucumberResourceIT.class, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.quarkiverse.cucumber.CucumberQuarkusTest;

public class CucumberResourceTest extends CucumberQuarkusTest {
public static void main(String[] args) {
public static void main(String... args) {
runMain(CucumberResourceTest.class, args);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.quarkiverse.cucumber.it.actors;

import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.is;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.MediaType;

import org.eclipse.microprofile.config.inject.ConfigProperty;

import io.quarkiverse.cucumber.it.CucumberResource;
import io.restassured.response.ValidatableResponse;

@ApplicationScoped
public class CucumberResourceActor {
private final String greeting;

@Inject
public CucumberResourceActor(@ConfigProperty(name = "greeting") String greeting) {
this.greeting = greeting;
}

private ValidatableResponse response;

public void callTarget() {
response = when().get(CucumberResource.PATH)
.then();
}

public void verifyResponse(int code) {
try {
response.statusCode(code)
.contentType(MediaType.TEXT_PLAIN)
.body(is(greeting));
} finally {
resetState();
}
}

private void resetState() {
response = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkiverse.cucumber.it.options;

import jakarta.enterprise.context.ApplicationScoped;

import io.quarkiverse.cucumber.CucumberOptions;
import io.quarkiverse.cucumber.CucumberQuarkusIntegrationTest;
import io.quarkiverse.cucumber.it.CucumberResourceIT;

@CucumberOptions(glue = { "io.quarkiverse.cucumber.it.steps" }, tags = "@important", plugin = { "json" })
@ApplicationScoped
public class CucumberOptionsIT extends CucumberQuarkusIntegrationTest {
public static void main(String... args) {
runMain(CucumberOptionsIT.class, args);
}

@Override
protected Package[] packagesToScanRecursively() {
return new Package[] { CucumberResourceIT.class.getPackage() };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import io.quarkiverse.cucumber.CucumberOptions;
import io.quarkiverse.cucumber.CucumberQuarkusTest;

@CucumberOptions(glue = { "io.quarkiverse.cucumber.it" }, tags = "@important", plugin = { "json" })
@CucumberOptions(glue = { "io.quarkiverse.cucumber.it.steps" }, tags = "@important", plugin = { "json" })
public class CucumberOptionsTest extends CucumberQuarkusTest {
public static void main(String[] args) {
public static void main(String... args) {
runMain(CucumberOptionsTest.class, args);
}
}
Loading

0 comments on commit 08f51ae

Please sign in to comment.