Invoke Struts Action Test from within a web app (JSF managed bean): TestCase.fname cannot be null - spring

I have a Junit 3.8 test of a Struts 2 action that runs with no problems from my workspace (from eclipse: right click > run as > junit test).
For this, I use two plugins:
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-junit-plugin</artifactId>
<version>2.1.8</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.1.8</version>
</dependency>
Here is the test class:
package com.myapp.user.my;
import org.apache.struts2.StrutsSpringTestCase;
import com.myapp.user.action.UserAction;
import com.opensymphony.xwork2.ActionProxy;
public class TestAccountActionUsingStrutsTestCase extends StrutsSpringTestCase {
public void testUserNameErrorMessage() throws Exception {
request.setParameter("userBean.userName", "Bruc");
request.setParameter("userBean.password", "test");
ActionProxy proxy = getActionProxy("/userAction");
UserAction userAction = (UserAction) proxy.getAction();
proxy.execute();
assertTrue("Problem There were no errors present in fieldErrors but there should have been one error present", userAction.getFieldErrors().size() == 1);
assertTrue("Problem field user.userName not present in fieldErrors but it should have been",
userAction.getFieldErrors().containsKey("userBean.userName") );
System.out.println("Finish 1 test.");
}
}
Next, I try to invoke this test, this time from within a web application (a JSF managed bean).
Here is my code for trying to do that (I'm calling the following runTest() method from a managed bean):
import java.util.List;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import com.myapp.user.my.TestAccountActionUsingStrutsTestCase;
public class CallStrutsActionExecuteThruTest {
public void runTest(){
System.out.println("CallStrutsActionExecuteThruTest.runTest() is executed.");
TestAccountActionUsingStrutsTestCase test = new TestAccountActionUsingStrutsTestCase();
JUnitCore jUnitCore = new JUnitCore();
Result result = jUnitCore.run(test);
List<Failure> list = result.getFailures();
for (Failure failure : list) {
System.out.println(failure.getMessage());
}
System.out.println("Test done!");
}
}
When I access the managed bean, I can see that runTest() is called. The first output CallStrutsActionExecuteThruTest.runTest() is executed. is printed to console. Strangely, the next outputs are not printed to console, although the debugger shows me they are executed.
Also, result.getFailures() returns a list with one element. As I said, its failure.getMessage() for some reason is not printed to console, but when I watch it in the debugger its value is TestCase.fname cannot be null.
* Even when I have only one method in my test class:
public void testTrue() throws Exception {
System.out.println("inside testTrue().");
assertTrue(true);
}
I still get the same results.
My questions are,
If I want to run the Struts action test from a JSF managed bean, am I using the Junit API correctly?
Why weren't the outputs that followed the first one printed to console?
How do I set TestCase.fname with a value? First I don't see a method in my test class to set this value. Second, from my understanding, fanme is the name of the test method in the test class that I want to call; and jUnitCore.run(test) should call all the test methods in the test class test, so how can I specify all these methods with only one fname parameter?
Download - you can download my project here. I use Maven, Eclipse, and deploy on Jboss 7.
I access the JSF managed bean by: http://localhost:8080/Struts2WithSpringDIIntegrationExampleJunitFromUI-1.0-SNAPSHOT/xhtml/hello.jsf

Struts2 tests don't work with raw parameters for some reason. Use parameterMap instead.
Map<String, String[]> parameterMap = new HashMap<String, String[]>();
parameterMap.put("userBean.userName", new String[]{"Bruc"});
parameterMap.put("userBean.password", new String[]{"test"});
StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
request.setupGetServletPath("/userAction");
request.setParameterMap(parameterMap);

Related

Equalsverifier fails when run with quarkus:dev

When running equalsverfier in quarkus dev mode, equalsverfier tests fail.
I tried to test a class with equalsverifier. This works in my IDE.
I tried to use it in quarkus dev mode (by running ./mvnw quarkus:dev), but then it fails with the following exception:
ERROR [io.qua.test] (Test runner thread) Test DingetjeTest#implementsEquals() failed
: java.lang.AssertionError: EqualsVerifier found a problem in class a.Dingetje.
-> Can not set final java.lang.String field a.Dingetje.text to a.Dingetje
For more information, go to: http://www.jqno.nl/equalsverifier/errormessages
at nl.jqno.equalsverifier.api.SingleTypeEqualsVerifierApi.verify(SingleTypeEqualsVerifierApi.java:308)
at a.DingetjeTest.implementsEquals(DingetjeTest.java:11)
Caused by: java.lang.IllegalArgumentException: Can not set final java.lang.String field a.Dingetje.text to a.Dingetje
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
at java.base/jdk.internal.reflect.UnsafeQualifiedObjectFieldAccessorImpl.get(UnsafeQualifiedObjectFieldAccessorImpl.java:38)
at java.base/java.lang.reflect.Field.get(Field.java:418)
at nl.jqno.equalsverifier.internal.reflection.FieldModifier.lambda$copyTo$1(FieldModifier.java:79)
at nl.jqno.equalsverifier.internal.reflection.FieldModifier.lambda$change$3(FieldModifier.java:113)
at nl.jqno.equalsverifier.internal.util.Rethrow.lambda$rethrow$0(Rethrow.java:47)
at nl.jqno.equalsverifier.internal.util.Rethrow.rethrow(Rethrow.java:30)
at nl.jqno.equalsverifier.internal.util.Rethrow.rethrow(Rethrow.java:45)
at nl.jqno.equalsverifier.internal.util.Rethrow.rethrow(Rethrow.java:55)
at nl.jqno.equalsverifier.internal.reflection.FieldModifier.change(FieldModifier.java:113)
at nl.jqno.equalsverifier.internal.reflection.FieldModifier.copyTo(FieldModifier.java:79)
at nl.jqno.equalsverifier.internal.reflection.InPlaceObjectAccessor.copyInto(InPlaceObjectAccessor.java:43)
at nl.jqno.equalsverifier.internal.reflection.InPlaceObjectAccessor.copy(InPlaceObjectAccessor.java:24)
at nl.jqno.equalsverifier.internal.checkers.ExamplesChecker.checkSingle(ExamplesChecker.java:84)
at nl.jqno.equalsverifier.internal.checkers.ExamplesChecker.check(ExamplesChecker.java:47)
at nl.jqno.equalsverifier.api.SingleTypeEqualsVerifierApi.verifyWithExamples(SingleTypeEqualsVerifierApi.java:413)
at nl.jqno.equalsverifier.api.SingleTypeEqualsVerifierApi.performVerification(SingleTypeEqualsVerifierApi.java:369)
at nl.jqno.equalsverifier.api.SingleTypeEqualsVerifierApi.verify(SingleTypeEqualsVerifierApi.java:304)
... 1 more
Here's the class under test:
package a;
import java.util.Objects;
public class Dingetje {
private final String text;
public Dingetje(String text) {
this.text = text;
}
#Override
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Dingetje)) {
return false;
}
Dingetje other = (Dingetje) o;
return text.equals(other.text);
}
#Override
public final int hashCode() {
return Objects.hash(text);
}
}
And the test:
package a;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;
class DingetjeTest {
#Test
void implementsEquals() {
EqualsVerifier.forClass(Dingetje.class)
.withNonnullFields("text")
.verify();
}
}
What am I missing here?
EqualsVerifier uses Objenesis to create instances of classes, and it keeps the same reference of the objenesis object around for performance reasons. It caches all the objects it has created before, so that makes things quicker when you want to create the same object over and over again, which EqualsVerifier tends to do.
However, EqualsVerifier keeps a static reference to objenesis, which means that it lives as long as the JVM does. It turns out that the Quarkus test runner can re-run the same tests again and again, and it creates a new class loader each time. But part of the equality of java.lang.Class is that the classloader that created the class, must also be the same. So it couldn't retrieve these objects from its cache anymore and returnd instances with classloaders that are now different from the other objects created in the test, and this caused the exceptions that you saw.
In version 3.8 of EqualsVerifier (created as a result of this StackOverflow post), this issue can be avoided by adding #withResetCaches() like this:
EqualsVerifier.forClass(Dingetje.class)
.withResetCaches()
.withNonnullFields("text")
.verify();
That fixes the problem.

How to fix 'java.lang.NoClassDefFoundError: org/apache/tinkerpop/gremlin/process/traversal/dsl/graph/GraphTraversalSource$GraphTraversalSourceStub'?

I am trying to initialize an in-memory graph using TinkerGraph.
Firstly, i have defined the bean in my context xml file and tried to initialise the TinkerGraph.
My intention is to unit test the classes that i have created for forming the gremlin queries, the end queries that i get from these classes are in the form of a string, so in order to execute them through the TinkerGraph, i have the used the approach given in the following post:
Get Gremlin query as a String and execute it in java without submitting it to the GremlinServer
I would also like to know whether the approach that i have taken is the preferred approach, for running the gremlin queries as part of the unit testing?
Following are the dependencies i have included in the pom.xml:
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>tinkergraph-gremlin</artifactId>
<version>3.2.4</version>
</dependency>
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-groovy</artifactId>
<version>3.0.2-incubating</version>
</dependency>
EmbeddedGremlinQueryEngine is as follows:
import org.apache.tinkerpop.gremlin.driver.Result;
import org.apache.tinkerpop.gremlin.driver.ResultSet;
import org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.util.List;
public class UcsEmbeddedGremlinQueryEngine implements GremlinEngine{
private static final Logger logger = LoggerFactory.getLogger(UcsEmbeddedGremlinQueryEngine.class);
private GraphTraversalSource graphTraversalSource = null;
private Graph graph = null;
private ScriptEngine engine = null;
private Bindings bindings = null;
public UcsEmbeddedGremlinQueryEngine() {
graph = TinkerGraph.open();
graphTraversalSource = graph.traversal();
engine = new GremlinGroovyScriptEngine();
bindings = engine.createBindings();
bindings.put("g", graphTraversalSource);
}
public void shutdown() throws Exception {
if (graph != null){
graph.close();
}
logger.info("TinkerGraph shutdown complete.");
}
#Override
public List<Result> query(String query) {
List<Result> res = null;
try {
ResultSet results = (ResultSet) engine.eval(query, bindings);
res = results.all().join();
for (Result r : res) {
System.out.println("result: " + r + '\n');
}
} catch (ScriptException e) {
e.printStackTrace();
}
return res;
}
// This function reads the initScript and run them as gremlin queries.
public synchronized void initialize() {
logger.debug("Initializing embedded TinkerGraph. This will only take a few seconds....");
//TODO include the execution of queries as part of initialisation
}
}
Stack trace is as follows:
java.lang.NoClassDefFoundError: org/apache/tinkerpop/gremlin/process/traversal/dsl/graph/GraphTraversalSource$GraphTraversalSourceStub
at org.apache.tinkerpop.gremlin.groovy.loaders.StepLoader.load(StepLoader.groovy:54)
at org.codehaus.groovy.vmplugin.v7.IndyInterface.selectMethod(IndyInterface.java:236)
at org.apache.tinkerpop.gremlin.groovy.loaders.GremlinLoader.load(GremlinLoader.groovy:28)
at org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine.<init>(GremlinGroovyScriptEngine.java:189)
at org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine.<init>(GremlinGroovyScriptEngine.java:172)
at com.intuit.gro.mcsdata.gemlinengine.UcsEmbeddedGremlinQueryEngine.<init>(UcsEmbeddedGremlinQueryEngine.java:28)
EmbeddedGremlinQueryEngine is defined as a bean in the xml file, when the bean is loaded i get the error as
Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/tinkerpop/gremlin/process/traversal/dsl/graph/GraphTraversalSource$GraphTraversalSourceStub
I don't understand how the GraphTraversalSourceStub comes into picture during initialization, I was not able to find any information about it.
Any help would be appreciated.
I think your problem is that you're:
using really really old versions of TinkerPop
the old versions you're using are probably incompatible
I'm not sure if you have a reason for using 3.2.4, but if so, make sure gremlin-groovy is also 3.2.4. Note that the 3.2.x line of code is largely not maintained at this point with the last release being 3.2.11 about 6 months ago. If you are developing a new application then I highly recommend that you simply utilize the latest version of 3.4.2 which released a few weeks ago.
As for your testing approach, I suppose that's fine. If you have test Gremlin strings then you really don't have much other choice in the matter, short of using Gremlin Server. Obviously providing a test harness for GremlinGroovyScriptEngine is a lot easier to do.
For version 3.4.0 you need at least the following classpath.
set cp=%cp%;C:\pathToM2Repo\org\apache\tinkerpop\gremlin-core\3.4.0\gremlin-core-3.4.0.jar
set cp=%cp%;C:\pathToM2Repo\org\apache\tinkerpop\gremlin-driver\3.4.0\gremlin-driver-3.4.0.jar
set cp=%cp%;C:\pathToM2Repo\org\apache\tinkerpop\gremlin-groovy\3.4.0\gremlin-groovy-3.4.0.jar
set cp=%cp%;C:\pathToM2Repo\org\apache\tinkerpop\gremlin-server\3.4.0\gremlin-server-3.4.0.jar
set cp=%cp%;C:\pathToM2Repo\org\apache\tinkerpop\gremlin-shaded\3.4.0\gremlin-shaded-3.4.0.jar
set cp=%cp%;C:\pathToM2Repo\org\apache\tinkerpop\tinkergraph-gremlin\3.4.0\tinkergraph-gremlin-3.4.0.jar
set cp=%cp%;C:\pathToM2Repo\org\apache\tinkerpop\tinkerpop\3.4.0\tinkerpop-3.4.0.jar
set cp=%cp%;C:\pathToM2Repo\commons-configuration\commons-configuration\1.10\commons-configuration-1.10.jar
set cp=%cp%;C:\pathToM2Repo\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar
set cp=%cp%;C:\pathToM2Repo\org\apache\logging\log4j\log4j-slf4j-impl\2.11.1\log4j-slf4j-impl-2.11.1.jar
set cp=%cp%;C:\pathToM2Repo\org\apache\logging\log4j\log4j-api\2.11.1\log4j-api-2.11.1.jar
set cp=%cp%;C:\pathToM2Repo\org\apache\logging\log4j\log4j-core\2.11.1\log4j-core-2.11.1.jar
set cp=%cp%;C:\pathToM2Repo\org\codehaus\groovy\groovy\2.5.4\groovy-2.5.4-indy.jar
set cp=%cp%;C:\pathToM2Repo\org\codehaus\groovy\groovy-json\2.5.4\groovy-json-2.5.4-indy.jar
set cp=%cp%;C:\pathToM2Repo\org\codehaus\groovy\groovy-xml\2.5.5\groovy-xml-2.5.5.jar
set cp=%cp%;C:\pathToM2Repo\org\codehaus\groovy\groovy-templates\2.5.5\groovy-templates-2.5.5.jar
set cp=%cp%;C:\pathToM2Repo\org\javatuples\javatuples\1.2\javatuples-1.2.jar
set cp=%cp%;C:\pathToM2Repo\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar
set cp=%cp%;C:\pathToM2Repo\io\netty\netty-all\4.1.31.Final\netty-all-4.1.31.Final.jar

Spring and Azure function

Does Spring work with Azure functions?
For example: Rest API that the code inside uses "Autowired" annotation (After running mvn azure-functions:run I've got NullPointerException on "myScriptService").
import java.util.*;
import com.microsoft.azure.serverless.functions.annotation.*;
import com.microsoft.azure.serverless.functions.*;
import com.company.ScriptService;
import org.springframework.beans.factory.annotation.Autowired;
/**
* Azure Functions with HTTP Trigger.
*/
public class Function {
#Autowired
ScriptService myScriptService;
/**
* This function listens at endpoint "/api/hello". Two ways to invoke it using "curl" command in bash:
* 1. curl -d "HTTP Body" {your host}/api/hello
* 2. curl {your host}/api/hello?name=HTTP%20Query
*/
#FunctionName("myhello")
public HttpResponseMessage<String> hello(
#HttpTrigger(name = "req",
methods = "post",
authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
context.getLogger().info("Java HTTP trigger processed a request.");
// Parse query parameter
String query = request.getQueryParameters().get("name");
String name = request.getBody().orElse(query);
if (name == null) {
return request.createResponse(400, "Please pass a name on the query string or in the request body");
} else {
return request.createResponse(200, "Hello, " + name + ", myScriptService.isEnabled(): " + myScriptService.isEnabled());
}
}
}
As some asked for a solution in the comments above, I'm assuming that this problem might be of relevance for other users, too.
So I think Spring Cloud Function is the magic word here: besides some other points (see the project page for details), it aims to enable Spring Boot features (like dependency injection, what you're looking for) on serverless providers (besides Azure Functions, also AWS Lambda and Apache OpenWhisk are supported).
So you have to make some modifications to your project:
Add the spring-cloud-function-adapter-azure dependency:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-azure</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
Your handler class needs some additional code:
Add the #SpringBootApplication annotation
Add the main() method known from Spring Boot applications
Make sure that Spring can find your ScriptService class e. g. by using the #ComponentScan annotation
It should look like this:
#SpringBootApplication
#ComponentScan(basePackages = { "package.of.scriptservice" })
public class Function {
#Autowired
ScriptService myScriptService;
#FunctionName("myhello")
public HttpResponseMessage<String> hello(
#HttpTrigger(name = "req", methods = "post", authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
// Your code here
}
public static void main(String[] args) {
SpringApplication.run(DemoFunctionHandler.class, args);
}
}
You can find a full example here and here
It looks like that there are a lot of changes between spring cloud v1 and v2. Have a quick look at this blog post: https://spring.io/blog/2018/09/25/spring-cloud-function-2-0-and-azure-functions
If you build your project like the example, spring will create the spring boot context when the azure function is called (and you call handleRequest). But the spring context is not available before this.
Do you add your package to scan for spring cloud function ?
spring.cloud.function.scan.packages="yourPackage"
It is to add in your application.properties

Unable to generate the Spring rest docs using Cucumber

I am trying to test spring rest documentation for rest API for our services using spring cucumber jvm but end up with a null pointer exeception when I try to execute the scenario, as the framework is not able to intialize the Junit context.
Error Message:
java.lang.NullPointerException at
org.springframework.restdocs.ManualRestDocumentation.beforeO‌​peration(ManualRestD‌​ocumentation.java:90‌​) at
org.springframework.restdocs.JUnitRestDocumentation.beforeOp‌​eration(JUnitRestDoc‌​umentation.java:76)
Code:
private AppProperties props;
#Before("#rest") public void beforeScenario() {
JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation( "target/generated-snippets" );
System.out.println( "jUnitRestDocumentation " +restDocumentation );
spec = new RequestSpecBuilder().addFilter( documentationConfiguration( restDocumentation ) ).build();
System.out.println( "\n spec init .. " +restDocumentation );
}
Step definition code:
#Given("^create a rest document for VHR API$")
public void create_a_rest_document_for_VHR_API() throws Throwable {
estAssured.given( spec )
.accept( "application/json" )
.filter( document( "vhrdocument" ) ) .when()
.get( props.getVhrrequesturl() + "/vhrData/{vehicleID}", "5VW4T7AU0FM029999" ) .then().log().all();
}
You aren't using JUnitRestDocumentation as it's intended to be used. It's designed to be used as a JUnit rule which means it should be a public field annotated with #Rule:
#Rule
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
Being a rule means that JUnit will automatically call restDocumentation for each test, allowing Spring REST Docs to set up and tear down the test-specific context. The NullPointerException is occurring because restDocumentation hasn't been called in this way and, therefore, the context hasn't been set up.
You haven't described how you're using Cucumber, but if you're using it's JUnit runner you should be able to fix the problem by declaring restDocumentation as a #Rule-annotated field as shown above. If you're not using its JUnit runner, you may need to use Spring REST Docs' ManualRestDocumentation instead. The Spring REST Docs reference documentation contains a section that describes how to set up your tests when you're not using JUnit.
I had the same problem because I had multiple test class inheriting the class, in which I declared the JUnitRestDocumentation instance. My mistake was that I declared the rule using the #Rule annotation. I should have used #ClassRule and declared the instance as static.
#ClassRule
public static JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
it happened with test SpockFramework, and i added to pom.xml:
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-junit4</artifactId>
<scope>test</scope>
</dependency>
I had the same symptoms when migrating from RestAssured 2.x to RestAssured 3.1.1.
The codebase had a way to setup RestAssured in order to avoid repetitive ceremony for every tests :
#Rule
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
#Before
public void configure_rest_assured() {
RestAssured.port = springServerPort;
RestAssured.config = config().objectMapperConfig(
objectMapperConfig().jackson2ObjectMapperFactory((cls, charset) -> customObjectMapper)
)
...;
RestAssured.requestSpecification = new RequestSpecBuilder()
.addRequestSpecification(documentationConfiguration(docRule, ...))
...
.build();
}
This was working well, until I migrated to 3.x. The issue was that new RequestSpecBuilder() will append itself to the default static RestAssured.requestSpecification.
The first test passed, but when it finished the rule was disposed (the after part), when the second test started to ran, the Before method was chaining
the specification created for the first test (referencing the disposed rule used by the first test method)
the specification created for the second test (referencing the active rule for second test method)
And so on as new tests are ran.
But when the second test is run RestAssured invoke specification in order, e.g. the number 1, but since it was referencing a disposed rule (the beforeOperation was executed on a null context)
To fix that the code had to clear the previous specifications :
#Before
public void configure_rest_assured() {
RestAssured.port = springServerPort;
RestAssured.config = config().objectMapperConfig(
objectMapperConfig().jackson2ObjectMapperFactory((cls, charset) -> customObjectMapper)
)
...;
RestAssured.requestSpecification = null; // avoid the builder to acquire previous specs.
RestAssured.requestSpecification = new RequestSpecBuilder()
.addRequestSpecification(documentationConfiguration(docRule, ...))
...
.build();
}
For using cucumber-java-8 with spring rest docs and spring-security the following worked for me.
This is combining #AndyWilkison's answer from above but using the cucumber hooks instead of junit rules.
public class StepDefs implements En {
#Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
private ManualRestDocumentation restDocumentation = new ManualRestDocumentation();
public StepDefs() {
BeforeStep((Scenario scenario) -> {
restDocumentation.beforeTest(AuthenticationStepDefs.class, scenario.getName());
mockMvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).apply(documentationConfiguration(restDocumentation)).build();
});
AfterStep((Scenario scenario) -> {
restDocumentation.afterTest();
});
When("create a rest document for VHR API", () -> {
MvcResult result = mockMvc.perform(/*
your normal call here
*/).
.andDo(document("documentation")).
.andReturn();
}
}
}

Running cucumber-groovy features against a spring boot api

I've been attempting to get cucumber-groovy working with spring-boot, but it's not been going well. I get the error org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8080/applicants": Connection refused; nested exception is java.net.ConnectException: Connection refused which seems to indicate that it's hitting the endpoint, but that the service isn't running.
I've read that I need to have a cucumber.xml file, but my project is not using any xml config, it's all annotations, so instead I've got this:
package support
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
#Configuration
#ComponentScan(basePackages = "com.base.package")
public class CucumberConfiguration {}
I've added it to the World, but this seems to be the wrong way of doing things (i.e. I don't know how to add an annotation on groovy step defs).
package support
import com.thing.app.Application
import org.junit.runner.RunWith
import org.springframework.boot.test.SpringApplicationContextLoader
import org.springframework.boot.test.WebIntegrationTest
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
import org.springframework.test.context.web.WebAppConfiguration
import static cucumber.api.groovy.Hooks.*
//#RunWith(SpringJUnit4ClassRunner)
//#ContextConfiguration(classes = Application, loader = SpringApplicationContextLoader)
//#WebAppConfiguration
//#WebIntegrationTest
#ContextConfiguration(classes = CucumberConfiguration)
public class AbstractTest {
}
World() {
new AbstractTest()
}
Before() {}
After() {}
I left in my other annotations to kind of show what I've done so far. None of it has worked.
I've also tried setting up an AbstractDefs class as seen here https://github.com/jakehschwartz/spring-boot-cucumber-example/tree/master/src/test/java/demo, but that also hasn't worked, mostly because I'm not using the cucumber-java style of things, but instead the cucumber-groovy style, which doesn't use step definition classes.
Edit: Just discovered I was doing things wrong by having an env.groovy, I'm used to the ruby cucumber, so I'm having trouble finding all the little problems. Still am having the same issue though, I don't know how to execute in a Spring context.
You can instantiate Spring test context with io.cucumber.spring.SpringFactory and register adapter in World to allow groovy script has access to Spring beans:
env.groovy:
#ContextConfiguration(classes = TestConfiguration, loader = SpringBootContextLoader)
class CucumberContextConfiguration {
}
//adapter bypassing World properties to Spring context
class SpringFactoryWorldAdapter {
private final SpringFactory factory;
SpringFactoryWorldAdapter(SpringFactory factory) {
this.factory = factory;
}
#Override
Object getProperty(String s) {
return factory.testContextManager.getContext().getBean(s);
}
}
def factory; //Keep state to prevent repeated context initialization
World { args ->
if (factory == null) {
factory = new SpringFactory()
factory.addClass(CucumberContextConfiguration)
factory.start()
}
new SpringFactoryWorldAdapter(factory)
}

Resources