Unit test which verifies only a few elements - java

There is an interface:
interface IEventListener
{
void onEvent(List <IEvent> events);
}
There is an event class:
class EventB
{
private final int id;
private final A a;
private final String somethingElse;
...
// constructors, getters
...
}
And there is a class to test:
class Doer
{
IEventListener eventListener;
void doSomething(Aaa a)
{
eventListener.onEvent(Arrays.asList(new EventA(1), new EventC(2)));
...
eventListener.onEvent(Arrays.asList(new EventB(42, a.getA(), "foo"), new EventA(3), new EventB(0, a.getA(), "bar")));
...
eventListener.onEvent(Arrays.asList(new EventC(4)));
}
}
The Doer is a code which I need to test, the method doSomething produces packs of events, and I need to test if it produces a particular event in some specific conditions.
More precisely I want to have a unit test which calls the method doSomething and checks that EventB is sent with "42" and A as from method argument a. All other events should be ignored.
To make such test I've only came up with solution involving quite verbose code with ArgumentCaptor, for-blocks, and magic boolean flags...
What is the best way to make a unit test for it? Maybe the code design is bad?

The design is correct, this is how you test it with Mockito:
import org.hamcrest.Matchers;
import org.mockito.Mockito;
public void firesEventsOnDoSomething() {
Listener listener = Mockito.mock(Listener.class);
Doer doer = new Doer(listener);
doer.doSomething(aaa);
Mockito.verify(listener).onEvent(
Mockito.argThat(
Matchers.hasItem(
Matchers.allOf(
Matchers.instanceOf(EventB.class),
Matchers.hasProperty("a", Matchers.equalTo(aaa.getA())),
// whatever you want
)
)
)
);
}
It's Mockito 1.9.0 and Hamcrest-library 1.2.1.
To use JUnit 4.10 together with Hamcrest-library 1.2.1 you should use junit:junit-dep:4.10 artifact, and exclude org.hamcrest:hamcrest-core from it:
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.10</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>

If you are using JUnit4 you can try a paremetrized test. Here there is an example http://www.mkyong.com/unittest/junit-4-tutorial-6-parameterized-test/.
If for each of your parameters you have to compare with a different result, you should better consider them as different test cases.

Create dummy implementation of EventListener:
class DummyEventListener implements EventListener {
private int expectedId;
DummyEventListener(int expectedId) {
this.expectedId = expectedId;
}
void onEvent(List <IEvent> events) {
for (IEvent event : events) {
if (!(event instanceof EventB)) {
continue;
}
EventB eb = (EventB)event;
assertEquals(expectedId, eb.getId());
// add more asserts here
}
}
}
Alternatively you can use one of available java mockup frameworks:
EasyMock, JMock, Mockito, JMockit etc.

Related

How to serialize list of enums from rest api

I want to return a list of enums from a rest api call, and have it show the value of the enum rather than just the enum names, in JSON format. Currently my rest call returns json looking like:
{
"responses": [
"ACTION_TAKEN",
"IGNORED",
"UNDETECTED"
]
}
But, I want it to be more like (or something like this):
{
"responses": [
{
"name":"ACTION_TAKEN",
"value":"Action Taken"
},
{
"name":"IGNORED",
"value":"Ignored"
},
{
"name":"UNDETECTED",
"value":"Undetected"
}
]
}
My enum looks like:
public enum Response {
ACTION_TAKEN ("Action Taken"),
IGNORED ("Ignored"),
UNDETECTED("Undetected");
private String value;
Response(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
#Override
public String toString() {
return value;
}
}
My model object looks like this. For the sake of this example, it just has a list of enum values.
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class StaticData {
private List<Response> responses;
public List<Response> getResponses() {
return responses;
}
public void setResponses(List<Response> responses) {
this.responses = responses;
}
}
The web service method looks like:
#Component
#Path("staticData")
#Produces("application/json")
#Consumes("application/json")
public class StaticDataResource {
#GET
public Response getCurrentContent() {
StaticData staticData = new StaticData();
staticData.setResponses(Arrays.asList(Response.values()));
return Response.ok(staticData).build();
}
}
Here are the dependencies from my effective pom (sorry for the bad formatting)
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.2.5.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
<version>1.19.1</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.thetransactioncompany</groupId>
<artifactId>cors-filter</artifactId>
<version>2.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.5.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.5.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.2.5.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>4.2.5.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.5.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.19</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.19</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.10</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>javax.ejb-api</artifactId>
<version>3.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
I have tried adding #JsonFormat(shape = JsonFormat.Shape.Object) to the top of my enum class. It didn't work. I tried adding #JsonValue to above my getValue() method in the enum class, and that didn't work either. I tried adding a custom serializer extending StdSerializer or JsonSerializer, and referencing that class above my enum using #JsonSerialize(using = ReasonSerializer). I put a breakpoint in the serialize method and didn't hit it, so that didn't work. I looked a little bit at doing implements ContextResolver<ObjectMapper>, but couldn't quite figure that out, or whether that was even the right path to go down or not.
Any help is greatly appreciated! Thanks!
Final solution
This works with JAX-RS and I have tested it with your code.
Use #JsonFormat with public String getName().
#JsonFormat(shape=JsonFormat.Shape.OBJECT)
public enum Response {
ACTION_TAKEN ("Action Taken"),
IGNORED ("Ignored"),
UNDETECTED("Undetected");
private String value;
Response(String value) {
this.value = value;
}
// Getters, Setters
public String getName() {
return name();
}
}
JSON output
{
"responses": [{
"value": "Action Taken",
"name": "ACTION_TAKEN"
}, {
"value": "Ignored",
"name": "IGNORED"
}, {
"value": "Undetected",
"name": "UNDETECTED"
}
]
}
Tested using the following dependency.
import com.fasterxml.jackson.annotation.JsonFormat;
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.6</version>
</dependency>
Alternative solution
Jackson docs states that behavior may change depending on what serializer is being used. So if the first solution doesn't work, this one might.
#JsonFormat(shape=JsonFormat.Shape.OBJECT)
public enum Response {
ACTION_TAKEN ("Action Taken"),
IGNORED ("Ignored"),
UNDETECTED("Undetected");
private String name;
private String value;
Response(String value) {
name = name();
this.value = value;
}
public String getName() {
return name;
}
public String getValue() {
return value;
}
}
According to this page:
https://www.javaworld.com/article/2072870/java-enums-are-inherently-serializable.html
Every Enum is naturally serializable, so yours should work too.
But, you should consider what was said here:
Is custom enum Serializable too?
Summary:
"Enum constants are serialized differently than ordinary serializable or externalizable objects. The serialized form of an enum constant consists solely of its name; field values of the constant are not present in the form"
To solve your problem I would consider using a simple Pojo class to represent your serializable class. If you still wants to use a Enum, then you could use a translator Pojo <-> Enum (I do not recomend doing this last option because of the redundancy, but last word should be yours depending on what you want to do with it :) ).

PowerMock + Emma - code coverage showing 0% for private static methods and other methods too [duplicate]

This question already has answers here:
PowerMock ECLEmma coverage issue
(9 answers)
Closed 4 years ago.
I have taken a reference of PowerMock from : Mock private method using PowerMockito and applied the same logic here. Also, I installed EMMA (open source tool) in eclipse/STS, but when I run the code I see zero % code coverage. why ?
public class MyClient {
public void publicApi() {
System.out.println("In publicApi");
int result = 0;
try {
result = privateApi("hello", 1);
} catch (Exception e) {
//Assert.fail();
}
System.out.println("result : "+result);
if (result == 20) {
throw new RuntimeException("boom");
}
}
private static int privateApi(String whatever, int num) throws Exception {
System.out.println("In privateAPI");
thirdPartyCall();
int resp = 10;
return resp;
}
private static void thirdPartyCall() throws Exception{
System.out.println("In thirdPartyCall");
//Actual WS call which may be down while running the test cases
}
}
MyClientTest.java
#RunWith(PowerMockRunner.class)
#PrepareForTest(MyClient.class)
public class MyClientTest {
#Test
public void testPublicAPI() throws Exception {
PowerMockito.mockStatic(MyClient.class);
//PowerMockito.doReturn(10).when(MyClient.class, "privateApi", anyString(), anyInt());
PowerMockito.when(MyClient.class,"privateApi", anyString(), anyInt()).thenReturn(anyInt());
}
}
Actual Code Coverage:
pom.xml
<dependencies>
<!-- Power Mock -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.7.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.7.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule-agent</artifactId>
<version>1.7.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>1.7.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
If you are constructing a Spy or Mock you are not invoking the actual code under test. The point of spies is to be able to verify() them, in order to check your code behaved correctly by invoking the right callbacks or methods. In the case of mocks the point is to steer code down a particular control flow path and also to verify() expected interactions with the mock.
Since your test case invokes the test method on a spy, it is therefore no wonder your code coverage is exactly 0%. If you were to verify your interactions with the mocked method, you'd probably find that none happened.
What you want to do instead is to setup your mocks but invoke the actual code under test 'the normal way'. The idea is to prime the execution environment, then invoke the tested method call 'normally', and finally observe what actually happened. That last bit consists of normal assertions on the produced output, verification of expected interactions (both that these took place, and also that these involved the expected arguments/values).
Change your test code:
MyClient classUnderTest = PowerMockito.spy(new MyClient());
To:
MyClient classUnderTest = new MyClient();
And watch the code coverage.

Spring 5 MVC JSON ser/deser not respecting properties (but works for XML)

I'm having a bizarre situation on a new setup using vanilla Spring Boot 2/Spring 5/Java 10/Jigsaw where no matter what I do, pulling an object through Spring MVC gives me an empty {} JSON object instead of my object properties.
BUT ... if I use an Accept header of application/xml instead of application/json, I get all the right properties. Maybe I'm losing my mind but I seem to recall in previous versions that if it worked on one side (xml), it should work on the other (json) and vice/versa.
I've traced it down internally to the BeanSerializer being created for my model class, with no properties. I'm just not sure why this is. I've traced through the execution to see Jackson is running during the http convert process ... it's just ignoring all properties inside the object.
Here's my setup:
Maven:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>10</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.4</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>2.9.6</version>
<scope>runtime</scope>
</dependency>
module-info:
module stupid.example {
opens com.example.microservice.datasynchronizer;
opens com.example.microservice.datasynchronizer.model;
opens com.example.microservice.datasynchronizer.webflux to spring.beans, spring.core, spring.web ;
opens com.example.microservice.datasynchronizer.dao to spring.core ;
requires java.base ;
requires java.xml.bind ;
requires spring.boot;
requires spring.boot.autoconfigure;
requires spring.beans ;
requires spring.context ;
requires spring.core ;
requires spring.data.commons ;
requires spring.web ;
requires spring.webmvc ;
requires java.persistence ;
requires org.junit.jupiter.api;
requires spring.test;
requires spring.boot.test ;
}
Model class (latest, with jaxb annotations just in case):
#Entity
#XmlRootElement
public class Thingamajig {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#XmlElement
private Long id;
#XmlElement
private String firstName;
#XmlElement
private String lastName;
public Thingamajig ( ) { ; }
public Thingamajig(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
#Override
public String toString() {
return String.format("Thingamajig [id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
}
}
Controller:
#RestController
public class ThingamajigController {
#Autowired
private ThingamajigDao _dao ;
#GetMapping("/thing/{id}")
public Thingamajig getPerson(#PathVariable Long id) {
Optional<Thingamajig> found = _dao.findById(id) ;
return found.get() ;
}
#PostMapping ( "/thing" )
#ResponseStatus(HttpStatus.CREATED)
public void add(#RequestBody Thingamajig person) {
_dao.save(person) ;
}
}
Configuration:
#EnableWebMvc
#SpringBootApplication
public class DataSynchronizerApplication {
public static void main(String[] args) throws Throwable {
SpringApplication.run(DataSynchronizerApplication.class, args);
}
}
What the heck am I missing? Any help appreciated.
You simply forgot to define getters and setters in your Thingamajig class.
XML works because you have defined the annotations on the attributes but JSON serializer is looking for getters.

Rest service won't work when using CDI: Service stays null

I am making a rest service application with JAX-RS. Its for some project for school. For this project I need to use follow techniques:
• Maven
• JAX-RS
• CDI
• JPA - EJB
• JNDI
• Bean Validation
So now I already maded my domain "Cafes" with a Fake DB ("CafeStub") and a real DB using JPA ("CafeDB"). My domain also makes a little usage of CDI. (#Inject in the CafeService class ...)
Non I wanted to create my rest service, using JAX-RS. This worked fine:
My problem is when I try to use CDI again it fails and it gives an 500 exception, NullPointerException, "Severe: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container"
Full Stacktrace:
I don't know how to fix this, already searched a long time .. Hopefully somebody can help me :s
This is my "CafeController" class. Producing the rest service
Path("/cafes")
public class CafeController {
#Inject
private CafeFacade cafeFacade;
public CafeController() {
//this.cafeFacade = new CafeService();
}
#GET
#Produces("application/json")
public Response getCafes(){
try{
// test ........
ObjectMapper mapper = new ObjectMapper();
Cafe cafe = cafeFacade.getCafe(new Long(1));
String jsonInString = mapper.writeValueAsString(cafe);
return Response.status(200).entity(jsonInString).build();
}catch (JsonProcessingException jsonEx) {
System.out.println("Json Exception");
System.out.println(jsonEx.getMessage());
return null;
}
}
This one is the "CafeService" class, the one who implemented "CafeFacade"
public class CafeService implements CafeFacade {
#Inject
private CafeRepository cafeRepository;
public CafeService() {
//cafeRepository = new CafeStub();
//cafeRepository = new CafeDB("CafesPU");
}
#Override
public long addCafe(Cafe cafe) {
return this.cafeRepository.addCafe(cafe);
}
#Override
public Cafe getCafe(long cafeID) {
return this.cafeRepository.getCafe(cafeID);
}
Her you see the "CafeStub" class, the one who implemented "CafeRepository"
public class CafeStub implements CafeRepository {
private static Map<Long, Cafe> cafes;
private static long counter = 0;
public CafeStub() {
cafes = new HashMap<Long, Cafe>();
// adding some dara
this.addSomeData();
}
#Override
public long addCafe(Cafe cafe) {
if(cafe == null){
throw new DBException("No cafe given");
}
counter++;
cafe.setCafeID(counter);
cafes.put(cafe.getCafeID(), cafe);
return cafe.getCafeID();
}
#Override
public Cafe getCafe(long cafeID) {
if(cafeID < 0){
throw new DBException("No correct cafeID given");
}
if(!cafes.containsKey(cafeID)){
throw new DBException("No cafe was found");
}
return cafes.get(cafeID);
}
At least here you can see my pom.xml (dependencies from CafeService project) - web.xml (from CafeService project) and project structure ...
<dependencies>
<dependency>
<groupId>Cafes</groupId>
<artifactId>Cafes</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19.4</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19.4</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19.4</version>
</dependency>
</dependencies>
Thanks in advance ...
Cheers
Tom
A class annotated with just #Path does not mark the class as a CDI bean as it is not in the list of bean defining annotations in the CDI spec. Adding RequestScoped to the REST service marks it as a CDI bean so injection works as you've discovered.
This answer here lists the annotations which mark a class as a CDI bean.
Is #javax.annotation.ManagedBean a CDI bean defining annotation?
Solved .. RequestScoped did the trick.. Daimn searched so long for one annotation.
#RequestScoped
#Path("/cafes")
public class CafeController {
Still I don't understand why I need to use it.
#RequestScoped : CDI instantiates and manages the bean
-> I thought my bean.xml would have instantiates and manages the bean ?

How to pass variables between cucumber-jvm steps

To pass variables between steps I have the step methods belong to the same class, and use fields of the class for the passed information.
Here is an example as follows:
Feature: Demo
Scenario: Create user
Given User creation form management
When Create user with name "TEST"
Then User is created successfully
Java class with steps definitions:
public class CreateUserSteps {
private String userName;
#Given("^User creation form management$")
public void User_creation_form_management() throws Throwable {
// ...
}
#When("^Create user with name \"([^\"]*)\"$")
public void Create_user_with_name(String userName) throws Throwable {
//...
this.userName = userName;
}
#Then("^User is created successfully$")
public void User_is_created_successfully() throws Throwable {
// Assert if exists an user with name equals to this.userName
}
My question is if it is a good practice to share information between steps? Or would be better to define the feature as:
Then User with name "TEST" is created successfully
In order to share commonalities between steps you need to use a World. In Java it is not as clear as in Ruby.
Quoting the creator of Cucumber.
The purpose of a "World" is twofold:
Isolate state between scenarios.
Share data between step definitions and hooks within a scenario.
How this is implemented is language specific. For example, in ruby,
the implicit self variable inside a step definition points to the
current scenario's World object. This is by default an instance of
Object, but it can be anything you want if you use the World hook.
In Java, you have many (possibly connected) World objects.
The equivalent of the World in Cucumber-Java is all of the objects
with hook or stepdef annotations. In other words, any class with
methods annotated with #Before, #After, #Given and so on will be
instantiated exactly once for each scenario.
This achieves the first goal. To achieve the second goal you have two
approaches:
a) Use a single class for all of your step definitions and hooks
b) Use several classes divided by responsibility [1] and use dependency
injection [2] to connect them to each other.
Option a) quickly breaks down because your step definition code
becomes a mess. That's why people tend to use b).
[1] https://cucumber.io/docs/gherkin/step-organization/
[2] PicoContainer, Spring, Guice, Weld, OpenEJB, Needle
The available Dependency Injection modules are:
cucumber-picocontainer
cucumber-guice
cucumber-openejb
cucumber-spring
cucumber-weld
cucumber-needle
Original post here https://groups.google.com/forum/#!topic/cukes/8ugcVreXP0Y.
Hope this helps.
It's fine to share data between steps defined within a class using an instance variable. If you need to share data between steps in different classes you should look at the DI integrations (PicoContainer is the simplest).
In the example you show, I'd ask whether showing "TEST" in the scenario is necessary at all. The fact that the user is called TEST is an incidental detail and makes the scenario less readable. Why not generate a random name (or hard code something) in Create_user_with_name()?
In Pure java, I just use a Singleton object that gets created once and cleared after tests.
public class TestData_Singleton {
private static TestData_Singleton myself = new TestData_Singleton();
private TestData_Singleton(){ }
public static TestData_Singleton getInstance(){
if(myself == null){
myself = new TestData_Singleton();
}
return myself;
}
public void ClearTestData(){
myself = new TestData_Singleton();
}
I would say that there are reasons to share information between steps, but I don't think that's the case in this scenario. If you propagate the user name via the test steps then it's not really clear from the feature what's going on. I think it's better to specifically say in the scenario what is expected. I would probably do something like this:
Feature: Demo
Scenario: Create user
Given User creation form management
When Create user with name "TEST"
Then A user named "TEST" has been created
Then, your actual test steps might look something like:
#When("^Create user with name \"([^\"]*)\"$")
public void Create_user_with_name(String userName) throws Throwable {
userService.createUser(userName);
}
#Then("^A user named \"([^\"]*)\" has been created$")
public void User_is_created_successfully(String userName) throws Throwable {
assertNotNull(userService.getUser(userName));
}
Here my way: I define a custom Scenario-Scope with spring
every new scenario there will be a fresh context
Feature #Dummy
Scenario: zweites Scenario
When Eins
Then Zwei
1: Use spring
<properties>
<cucumber.version>1.2.5</cucumber.version>
<junit.version>4.12</junit.version>
</properties>
<!-- cucumber section -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-spring</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<!-- end cucumber section -->
<!-- spring-stuff -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.4.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.4.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.4.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.4.RELEASE</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.4.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
<version>2.4.0.RELEASE</version>
<scope>test</scope>
</dependency>
2: build custom scope class
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
#Component
#Scope(scopeName="scenario")
public class ScenarioContext {
public Scenario getScenario() {
return scenario;
}
public void setScenario(Scenario scenario) {
this.scenario = scenario;
}
public String shareMe;
}
3: usage in stepdef
#ContextConfiguration(classes = { CucumberConfiguration.class })
public class StepdefsAuskunft {
private static Logger logger = Logger.getLogger(StepdefsAuskunft.class.getName());
#Autowired
private ApplicationContext applicationContext;
// Inject service here : The impl-class need #Primary #Service
// #Autowired
// IAuskunftservice auskunftservice;
public ScenarioContext getScenarioContext() {
return (ScenarioContext) applicationContext.getBean(ScenarioContext.class);
}
#Before
public void before(Scenario scenario) {
ConfigurableListableBeanFactory beanFactory = ((GenericApplicationContext) applicationContext).getBeanFactory();
beanFactory.registerScope("scenario", new ScenarioScope());
ScenarioContext context = applicationContext.getBean(ScenarioContext.class);
context.setScenario(scenario);
logger.fine("Context für Scenario " + scenario.getName() + " erzeugt");
}
#After
public void after(Scenario scenario) {
ScenarioContext context = applicationContext.getBean(ScenarioContext.class);
logger.fine("Context für Scenario " + scenario.getName() + " gelöscht");
}
#When("^Eins$")
public void eins() throws Throwable {
System.out.println(getScenarioContext().getScenario().getName());
getScenarioContext().shareMe = "demo"
// you can save servicecall here
}
#Then("^Zwei$")
public void zwei() throws Throwable {
System.out.println(getScenarioContext().getScenario().getName());
System.out.println(getScenarioContext().shareMe);
// you can use last service call here
}
#Configuration
#ComponentScan(basePackages = "i.am.the.greatest.company.cucumber")
public class CucumberConfiguration {
}
the scope class
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;
public class ScenarioScope implements Scope {
private Map<String, Object> objectMap = Collections.synchronizedMap(new HashMap<String, Object>());
/** (non-Javadoc)
* #see org.springframework.beans.factory.config.Scope#get(java.lang.String, org.springframework.beans.factory.ObjectFactory)
*/
public Object get(String name, ObjectFactory<?> objectFactory) {
if (!objectMap.containsKey(name)) {
objectMap.put(name, objectFactory.getObject());
}
return objectMap.get(name);
}
/** (non-Javadoc)
* #see org.springframework.beans.factory.config.Scope#remove(java.lang.String)
*/
public Object remove(String name) {
return objectMap.remove(name);
}
/** (non-Javadoc)
* #see org.springframework.beans.factory.config.Scope#registerDestructionCallback(java.lang.String, java.lang.Runnable)
*/
public void registerDestructionCallback(String name, Runnable callback) {
// do nothing
}
/** (non-Javadoc)
* #see org.springframework.beans.factory.config.Scope#resolveContextualObject(java.lang.String)
*/
public Object resolveContextualObject(String key) {
return null;
}
/** (non-Javadoc)
* #see org.springframework.beans.factory.config.Scope#getConversationId()
*/
public String getConversationId() {
return "VolatileScope";
}
/**
* vaporize the beans
*/
public void vaporize() {
objectMap.clear();
}
}
Other option is to use ThreadLocal storage. Create a context map and add them to the map. Cucumber JVM runs all the steps in the same thread and you have access to that across all the steps. To make it easier, you can instantiate the storage in before hook and clear in after hook.
If you are using Serenity framework with cucumber you can use current session.
Serenity.getCurrentSession()
more about this feature in http://thucydides-webtests.com/2012/02/22/managing-state-between-steps/. (Serenity was called Thucydides before)

Categories