i am trying to set up testing platform for our new project.
We want to use Spock 2 with JUnit 5.
And now i want to use #ExtendWith annotation in Spock based test.
Here is an example:
#SpringBootTest(classes = ExtensionTest.class)
#ExtendWith(ClassExtension.class)
class ExtensionTest extends Specification {
void "test"() {
when:
true
then:
noExceptionThrown()
}
}
Given Extension (ClassExtension) is implementing interfaces: BeforeAllCallback and AfterAllCallback.
class ClassExtension implements BeforeAllCallback, AfterAllCallback {
#Override
void beforeAll(ExtensionContext extensionContext) throws Exception {
...
}
#Override
void afterAll(ExtensionContext extensionContext) throws Exception {
...
}
}
Unfortunately it doesn't work. Extension is not being run.
What am I doing wrong?
Spock 2.x does not use the Jupiter Engine but implements its own SpockEngine and its own SpockExecutionContext based on the Jupiter API. Obviously it does not support Jupiter extensions, probably because Spock has its own extension mechanisms for annotation-driven and global extensions.
Bottom line: I recommend to port your JUnit Jupiter extension to a Spock extension. There you also have better means to hook into the Spock lifecycle.
The thread is old, but in case anyone would be searching for the similar problem, there is a way now to use Junit 5 extensions with Spock 2: spock-junit5.
I did not try it with spring boot extension, but it should work because all required jupiter extension interfaces supported by the library.
Related
My unit test in optaplanner-rewrite has this code using OpenRewrite bom 1.11.0:
#Override
public void defaults(RecipeSpec spec) {
spec.recipe(new AsConstraintBuilder())
.parser(JavaParser.fromJavaVersion()
.classpath("optaplanner-core"));
}
and it runs fine in Maven.
But when I run the unit test in my IDE (IntelliJ), I get this error:
java.lang.IllegalArgumentException: Unable to find runtime dependencies beginning with: 'optaplanner-core'
at org.openrewrite.java.JavaParser.dependenciesFromClasspath(JavaParser.java:97)
at org.openrewrite.java.JavaParser$Builder.classpath(JavaParser.java:250)
This is because my IDE window has a multiproject open, that includes both optaplanner-core and optaplanner-rewrite. There is no optaplanner-core.jar in the classpath of my test run, only optaplanner-core/target/classes.
How do I run my unit test in IntelliJ?
This is not a proper solution, but as a workaround you should be able to load the entire classpath like this:
#Override
public void defaults(RecipeSpec spec) {
List<Path> classpath = new ClassGraph().getClasspathURIs().stream().map(Paths::get).toList();
spec.recipe(new AsConstraintBuilder())
.parser(JavaParser.fromJavaVersion()
.classpath(classpath));
}
I am trying to call my simulation model from another java program. I followed the official instructions to have the codes like below:
package test;
public class UserMain {
public UserMain(){};
public void start(){
String[] args = new String[]{"D:\\user\\model\\Repast_java\\IntraCity_Simulator\\IntraCity_Simulator.rs"};
// repast.simphony.runtime.RepastMain.main(args);
}
public static void main(String[] args) {
UserMain um = new UserMain();
um.start();
}
}
It didn't work. I think it's due to the wrong classpath. How to configure it correctly?
Note that you need to have repast.simphony.runtime/bin and the jars in repast.simphony.runtime/lib on your classpath since the runtime needs these to start.
This is more of a Java or Eclipse question about how to use Java's class path. But briefly, if you are running from the command line, you can use the -cp argument to specify the classpath. A quick google should provide the details. In Eclipse, the classpath is specified in dependencies tab in the Run Configuration (Run -> Run Configurations) for your application.
I managed to run Karate tests using Junit. But what I want is to run Karate from java application instead of Junit runner.
Currently I'am running from JUnit:
import com.intuit.karate.cucumber.CucumberRunner;
import com.intuit.karate.cucumber.KarateStats;
import cucumber.api.CucumberOptions;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
#CucumberOptions(tags = {"~#ignore"})
public class TestParallel {
#Test
public void testParallel() {
KarateStats stats = CucumberRunner.parallel(getClass(), 5, "target/surefire-reports");
assertTrue("scenarios failed", stats.getFailCount() == 0);
}
}
I tried calling the Junit class from my application main using the code below:
JUnitCore junit = new JUnitCore();
Result result = junit.run(TestParallel.class);
But I have this error:
java.lang.NoClassDefFoundError: com/intuit/karate/cucumber/CucumberRunner
Yes, please use the Java API, documented here: https://github.com/intuit/karate#java-api
Note that you won't get reports if you go down this path.
I solved the problem by removing the test scope from the karate dependencies in the pom.
Everything works fine including the reports and the output.
Trying to implement selenium + Cucumber + Testng instead of Junit.
My queries are
What is the alternate for #Runwith(Cucumber.class) in testng
How to run the class file which contains the path to feature file
package runner;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
#CucumberOptions(features="src/main/java/testCases/Cucumber/Login_Cucumber.Feature",glue="")
public class TestRunner extends AbstractTestNGCucumberTests {
}
TestNg uses #CucumberOptions tag to declare parameters
#CucumberOptions(plugin = "json:target/cucumber-report.json")
public class RunCukesTest extends AbstractTestNGCucumberTests {
}
or
#CucumberOptions(features = "src/test/resources/features/Download.feature",
glue = "uk.co.automatictester.jwebfwk.glue",
format = {"pretty"})
Check this out: https://github.com/cucumber/cucumber-jvm/tree/master/examples/java-calculator-testng
Also a possible dup of :How to integrate the cucumber in testNG?
Install TestNG Eclipse Plugin. Afterwards you should be able to run TestNG Test.
First of all, Cucumber have .feature files and not test files.
Answer to your first question: 1. What is the alternate for #Runwith(Cucumber.class) in testng? "You don't need #RunWith while running with TestNG"
I didn't understand your second question but you need to understand that Cucumber runs end execute the Runner class by default and you have already defined feature files in #CucumberOptions section.
To make it more clear you can easily implement and Run Cucumber project using TestNG. The whole game is in your pom.xml file and Runner class.
Following detail also explains that you can run each scenario in cucumber as a test using TestNG.
How? It's explained below:
First of all, update your Cucumber Maven dependencies from info.cukes to io.cucumber dependencies
Following Java code in Cucumber Runner Class worked perfectly for me to run each scenario as TestNG test in feature files:
#CucumberOptions(features = "src/test/resources", plugin = "json:target/cucumber-report-feature-composite.json")
public class TestRunner {
private TestNGCucumberRunner testNGCucumberRunner;
#BeforeClass(alwaysRun = true)
public void setUpClass() throws Exception {
testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
}
#Test(groups = "cucumber scenarios", description = "Runs Cucumber
Scenarios", dataProvider = "scenarios")
public void scenario(PickleEventWrapper pickleEvent, CucumberFeatureWrapper
cucumberFeature) throws Throwable{
testNGCucumberRunner.runScenario(pickleEvent.getPickleEvent());
}
#DataProvider
public Object[][] scenarios() {
return testNGCucumberRunner.provideScenarios();
}
#AfterClass(alwaysRun = true)
public void tearDownClass() throws Exception {
testNGCucumberRunner.finish();
}
}
Run with mvn clean test command and see the magic :)
I would be happy to see your problem resolved. Please let me know if this issue is still not resolved.
Reference: https://github.com/cucumber/cucumber-jvm/blob/master/testng/README.md
I followed this approach: https://github.com/cucumber/cucumber-jvm/blob/master/examples/java-calculator-testng/src/test/java/cucumber/examples/java/calculator/RunCukesByCompositionTest.java
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
#CucumberOptions(features="src/test/resources/features",glue="stepDefinitions",tags="#Test01",plugin= {"pretty", "html:target/cucumber-reports" },monochrome=true)
public class RunnerTest extends AbstractTestNGCucumberTests{
}
It will work for sure.
This perfectly worked for me
package com.shyom.cucumberOptions;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
#CucumberOptions(
plugin = "json:target/cucumber-report.json",
features = "/shyom/src/test/java/feature",
glue="stepDefinations"
)
public class TestRunner extends AbstractTestNGCucumberTests{
}
I want to know if you guys know how to make BDD tests with Robotium.
As I research Robotium works with a different Virtual Machine (Dalvik) so I cannot run as Junit Test (Only with Android Junit Test). So I found a possible solution to run Robotium with Junit with RoboRemote https://github.com/groupon/robo-remote. But when i tried to integrate with cucumber the tests became unstable.
So you guys know some way to make BDD tests using Robotium?
I have successfully integrated Robotium using Cucumber-JVM for Android.
For information about the now official cucumber-android module for Cucumber-JVM and the installation, have a look here. You can also find API-documentation and examples about Cucumber-JVM here: http://cukes.info/platforms.html.
In your test-module for your app, simply add the Robotium Solo jar-file as a dependency (Scope: Compile).
One of my test-classes looks like this:
public class CucumberSteps extends ActivityInstrumentationTestCase2<YourActivity> {
private Solo solo;
public CucumberSteps() {
super(YourActivity.class);
}
#Override
protected void setUp() throws Exception {
super.setUp();
}
#Before
public void before() {
solo = new Solo(getInstrumentation(), getActivity());
}
#After
public void after() throws Throwable {
//clean up
solo.finalize();
}
#Override
protected void tearDown() throws Exception {
solo.finishOpenedActivities();
super.tearDown();
}
#Given("^step_given_description$")
public void step_given_description() throws Throwable {
final View testView = solo.getView(R.id.testView);
solo.waitForView(testView);
solo.clickOnView(testView);
// and so on
}
}
I hope this is enough information for anyone to get started. When this question was asked, cucumber-android didn't exist yet. Keep in mind though, GUI tests are very often somewhat unstable! I managed to get a stable set of tests locally, but e.g. in Jenkins, usually some tests fail for unknown reasons.
I know it's a very old questions but the documentation on this subject is very limited so I'll post another piece of information.
This article helped me a lot: http://www.opencredo.com/2014/01/28/cucumber-android-vs-cucumber-appium/
And I used the documentation here as well (I know it's deprecated but the main project has no docs about android at all): https://github.com/mfellner/cucumber-android.
I got it to work with IntelliJ 13 Community Edition and Maven using bits from Android
Bootstrap - http://www.androidbootstrap.com/ (Mostly the Maven integration test project configuration)
Hope it helps.
I got working robotium with cucumber-jvm and androidstudio using this runner:
import android.os.Bundle;
import cucumber.api.CucumberOptions; import
cucumber.api.android.CucumberInstrumentation;
#CucumberOptions(features = {"features"}, tags = {"#smoke",
"~#pending", "~#manual", "~#reboot"})
public class Instrumentation extends CucumberInstrumentation {
private final CucumberInstrumentation instrumentationCore = new CucumberInstrumentation();
#Override
public void onCreate(final Bundle bundle) {
super.onCreate(bundle);
instrumentationCore.onCreate(bundle);
start();
}
#Override
public void onStart() {
waitForIdleSync();
instrumentationCore.start();
}
}