Perform action if Maven / Cucumber test fails? - java

I have a set of cucumber tests ran in Maven job. How would I perform a specific action if the test fails?
RunnerTest.Java
import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#Cucumber.Options(format = {"html:target/test-report", "json:target/test-report.json", "junit:target/test-report.xml"},
features = "src/test/resource"
)
public class RunnerTest {
}

I think you should use the TestWatcher rule.
More information here : JUnit on failure callback/method

Related

Unable to run any Junit4 tests - InvalidTestClassError: Invalid test class

I am trying to run Junit4 tests and I am unable to run it. I have the following dependency installed
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
My test class looks like this
package model.validators;
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
class UserValidatorTest {
#Test
public void shouldReturnTrueForValidPhoneNumbers() {
List<String> phoneNumbers = Arrays.asList(
"9876543210",
"7777543210"
);
boolean result = UserValidator.validateUserPhoneNumbers(phoneNumbers);
Assert.assertTrue(result);
}
}
When I try to run this test, I get the following error
org.junit.runners.model.InvalidTestClassError: Invalid test class 'model.validators.UserValidatorTest':
I am using IntellijIdea. Any idea what is going wrong here ? TIA
Tried changing dependencies, reloading maven project, setting the correct classpath in Junit Run configurations
I can see that your class UserValidatorTest is not public. On making your class public, you will be able to run the tests.
package model.validators;
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
public class UserValidatorTest {
#Test
public void shouldReturnTrueForValidPhoneNumbers() {
List<String> phoneNumbers = Arrays.asList(
"9876543210",
"7777543210"
);
boolean result = UserValidator.validateUserPhoneNumbers(phoneNumbers);
Assert.assertTrue(result);
}
}
JUnit4 requires everything to be public.
JUnit5 is more tolerant regarding the visibilities of Test classes (Test classes, test methods, and lifecycle methods are not required to be public, but they must not be private).
SonarLint Rule description:
In this context (JUnit5), it is recommended to use the default package visibility, which improves the readability of code.

io.cucumber.core.exception.CucumberException: java.lang.NoClassDefFoundError: io/cucumber/core/internal/gherkin/ast/Node

When I have run my below snippets by using Run as 'JUnit Test'.I am facing the below error message PFA
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
#RunWith(Cucumber.class)
#CucumberOptions(
features= {"src/test/resources/AppFeatures"},
glue= {"stepdefinitions", "AppHooks"},
plugin= {"pretty",
"com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:",
"timeline:test-output-thread/"
} //pretty keyword used for printing purpose
)
public class MyTestRunner {
}
Are you using Beforeclass and Afterclass hooks? If yes, please share them.
If not, please try changing glue path to:
glue = "stepdefinitions"
Glue path is supposed to specify the path to step definitions only.

Jenkins: Cucumber Test Runners execution order

In my automation project I have two test runners:
#RunWith(Cucumber.class)
#CucumberOptions(features = ".",
plugin = {"json:target/json","rerun:rerun.txt","io.qameta.allure.cucumberjvm.AllureCucumberJvm"})
public class MainTest {
}
#RunWith(Cucumber.class)
#CucumberOptions(features = "#rerun.txt",
plugin = {"json:target/json","rerun:rerun.txt","io.qameta.allure.cucumberjvm.AllureCucumberJvm"})
public class FailedScenarioRerunTest {
}
I want to run first MainTest and next FailedScenarioTest, how to do it?
In theory test should run with features alphabetical order, at local machine it's exactly like that, but in Jenkins order is not alphabetical.
anyone had to deal with such a problem?
Answer is:
<runOrder>alphabetical</runOrder>
in surefire configuration in pom.xml

PowerMock ECLEmma coverage issue

We are using EasyMock and PowerMock with JUnit. The coverage tool used is ECLEmma. With EasyMock, it shows the coverage properly in green (as covered). However, for the code that is unit tested with PowerMock, the coverage is shown in red (uncovered). Have read similar questions on the web. However, just wanted to check if there is a solution for this.
Thanks
Venkatesh
Yes, there is a solution for this:
First you will have to add this maven dependency:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule-agent</artifactId>
<version>1.6.4</version>
<scope>test</scope>
</dependency>
Then, instead of using this annotation #RunWith(PowerMockRunner.class), just add a #Rule in the Test class like this:
public class Test {
#Rule
public PowerMockRule rule = new PowerMockRule();
you can find more in this blog Make EclEmma test coverage work with PowerMock
It's a known problem : https://github.com/jayway/powermock/issues/422
And it has been for a long time, it won't be fixed anytime soon.
I suggest you use eCobertura instead.
This has worked in most cases in my project:
#Rule
public PowerMockRule rule = new PowerMockRule();
static {
PowerMockAgent.initializeIfNeeded();
}
Remove/Comment #RunWith(PowerMockRunner.class) & include following imports after adding powermock-module-javaagent-1.6.5.jar in your classpath:
import org.junit.Rule;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.powermock.modules.agent.PowerMockAgent;
Now right click->Coverage As->Coverage Configurations and add following lines in Arguments:
-ea -noverify -javaagent:path/to/powermock-module-javaagent-1.6.5.jar
Click Apply->Coverage.
Also note that #Before would not work in this case so you have to add all the stuffs in the methods marked with #Test from the method marked with #Before.
We have a static classes to mock. With mocking static classes, eclEmma code coverage plugin is not working in Eclipse. So what we did is, so placed #RunWith(JUnit4.class) (Instead of #RunWith(PowerMockRunner.class) ) before class and placed following lines inside class
static {
PowerMockAgent.initializeIfNeeded();
}
#Rule
public PowerMockRule rule = new PowerMockRule();
Compiled the class and ran the test class. Code coverage is working for class. This change is only in Eclipse IDE.
After writing test cases, we reverted code back to normal. Placed #RunWith(PowerMockRunner.class) instead of #RunWith(JUnit4.class) and commented above static code and powermockrule lines.
I have managed to generate PowerMock coverage with Jacoco, using powermock-module-javaagent.
Just make sure you put powermock agent after jacoco agent:
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>true</useSystemClassLoader>
<argLine>${jacocoArgLine} -javaagent:${settings.localRepository}/org/powermock/powermock-module-javaagent/${powermock.version}/powermock-module-javaagent-${powermock.version}.jar -noverify</argLine>
...
If you want to see an example, take a look at this project: https://github.com/jfcorugedo/sonar-scanner
Here you can see that sonar takes into account static methods and new statements mocked by PowerMock:
If you want to mock newstatements make sure you use PowerMockRule instead of PowerMockRunner.
Take a look at this test
Updating powermock version fix my issue below is maven dependency of supported version
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule-agent</artifactId>
<version>1.7.3</version>
<scope>test</scope>
</dependency>
Hope this helps !!!
I was facing the same issue. So, I updated the powerMockito version. Now I am using Power mock version 1.7.4 and Jacoco version 0.8.5. It's even working on eclipse also.
Here is some more detailed answer with full class.
couple of points to note:
I had to use spy instead of mockStatic
I had to move #PrepareForTest to method level.
As someone mentioned in other answers I also had to add following dependency
org.powermock:powermock-module-junit4-rule-agent:2.0.9
Below is my full class code for reference:
import java.util.ArrayList;
import java.util.Arrays;
import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.agent.PowerMockAgent;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.opensource.dummy.config.DummyConfig;
import org.opensource.dummy.data.builder.DataBuilder;
import org.opensource.dummy.model.SampleRequest;
import org.opensource.dummy.model.SampleType;
import org.opensource.dummy.model.Item;
import org.opensource.dummy.model.RequestType;
#RunWith(JUnit4.class)
public class MockedDataSearchServiceHelperTest {
static {
PowerMockAgent.initializeIfNeeded();
}
#Rule
public PowerMockRule rule = new PowerMockRule();
#PrepareForTest({ DummyConfig.class })
#SuppressWarnings("unchecked")
#Test
public void testSearchResponseValid() throws Exception {
Item Item = DataBuilder.createItem("2024-01-24", "2024-12-25", "61ef8faebec3bb72fbcf336d", null);
SampleRequest sampleRequest = DataBuilder.createSampleRequest(Arrays.asList(Item),
DataBuilder.createDeliveryMetrics(1), null, RequestType.ITEM);
BoolQueryBuilder boolQueryBuilder = DataSearchServiceHelper.createCustomQuery(sampleRequest, Item);
SearchRequestBuilder searchRequestBuilder = PowerMockito.mock(SearchRequestBuilder.class);
ActionFuture<SearchResponse> actionFuture = PowerMockito.mock(ActionFuture.class);
Client client = PowerMockito.mock(Client.class);
PowerMockito.spy(DummyConfig.class);
PowerMockito.doReturn(client).when(DummyConfig.class, "getClient");
Mockito.when(client
.prepareSearch(new String[] { "dummy" }))
.thenReturn(searchRequestBuilder);
Mockito.when(searchRequestBuilder.setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN))
.thenReturn(searchRequestBuilder);
Mockito.when(searchRequestBuilder.setQuery(Mockito.any(QueryBuilder.class))).thenReturn(searchRequestBuilder);
Mockito.when(searchRequestBuilder.execute()).thenReturn(actionFuture);
Mockito.when(actionFuture.actionGet()).thenReturn(new SearchResponse(null, null, 0, 0, 0, 0, null, null));
SearchResponse searchResponse = DataSearchServiceHelper.getSearchResponse(
new String[] { "dummy" },
boolQueryBuilder, DataBuilder.createDeliveryMetrics(1), RequestType.ITEM);
Assert.assertNotNull(searchResponse);
}
}
I hope this helps to someone.
For mocking static classes, using #RunWith(PowerMockRunner.class) and running the "Coverage As JUnit Test" on Eclipse does show covered code as uncovered and it clearly does seem like an issue.
To add to the solutions above, in a maven project, you can try this..
In the root pom.xml, for report generation, add html as a format in cobertura-maven-plugin. Below is the way it looks.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
</configuration>
</plugin>
Then, go to the module where your class resides and open target/site/cobertura/index.html file in Eclipse Web Browser or in the one of your choice. You can find the coverage information there.

BDD Cucumber tests via jUnit

I there any examples for runing cucumbers via jUnit manually?
I have a simple empty class with #RunWith(Cucumber.class) which has all my feature files.
import cucumber.junit.Cucumber;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
public class RunCukesTest {
}
And simple junit test running it:
#Test
public void cucumberFirstPartTests()throws Exception{
Cucumber cucumber = new Cucumber(RunCukesFirstPart.class);
RunNotifier notifier = new RunNotifier();
cucumber.run(notifier);
}
Is there any examples at all for filtering manually tests, using runner scheduler and descriptions of tests for cucumbers? Watched documentation, but for me it is not enough. I will appreciate any links. Thank you.
You can mark each Scenario/Feature with any number of tags, using #TAGNAME
Given that, you can tell the runner to run a selected set of tags
#RunWith(Cucumber.class)
#Cucumber.Options(tags = {"#TAGNAME"})
Is that what you were after?

Categories

Resources