Is it possible to use JBehave for BDD testing in mule application? Any working example will be very helpful.
Thank you :)
should be possible. What do you want to test? It's easy to test a single Java Transformer with JBehave, but it's getting worse when you start writing integration tests with JBehave. Seriously I won't do that.
It could work if you use MUnit with Java, but I would never ever mix Java JBehave stuff with XML MUnit tests because it will become unmaintainable.
I always test without a BDD tool as wrapper and use simple Given-When-Then-like syntax as names of my tests. For example: "should-be-irrelevant-when-purchaser-is-zero" is a name of one of my test. By using this you always see which test fails why.
looking forward to your response
In case you want to test a custom Java transformer like this one:
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractTransformer;
public class MyCustomTransformer extends AbstractTransformer {
#Override
protected Object doTransform(Object src, String enc) throws TransformerException {
return null;
}
}
It's definitely possible, but I don't see why it should be a benefit. I would use Mockito with Given/When/Then syntax instead.
Related
How do I define my own StepActions class in a Karate test?
All I need is one working example (apparently nobody has an example of this anywhere; I looked and couldn't find anything and so I am asking here).
For example, how would I implement this helper step action in Java? In this case, how do I get access to the WebDriver driver instance from within Java context? Then, how do I call the embed?
#Slf4j
public class SeleniumStepActions extends StepActions {
public SeleniumStepActions(FeatureContext featureContext, CallContext callContext, Scenario scenario, LogAppender appender) {
super(featureContext, callContext, scenario, appender);
}
#When("^screenshot$")
public void takeAScreenShot()
{
// goals is to simulate this in a karate js test
// * def bytes = driver.screenshot()
// * karate.embed(bytes, 'image/png')
log.info("Testing my own custom action.");
}
}
It is possible the above won't work. I am just looking to be pointed in the right direction by someone who knows. I wish there was such an example in the karate demo.
You can't. Which is why there ain't any demo :P
For a detailed discussion, please read this thread: https://github.com/intuit/karate/issues/398
The summary:
Karate does not support the "Step Definitions" that Cucumber does
There are 2 ways to inject custom logic, a) JS, b) Java interop
These are more than sufficient to implement something close to custom "keywords" - see this example: https://twitter.com/KarateDSL/status/1128170638223364097
and another: https://twitter.com/KarateDSL/status/1144458169822806016
If you insist on making your test read like "plain english" (which IMHO is not worth it) - then Karate may not be the best choice for your team.
How do I define my own StepActions class in a Karate test?
All I need is one working example (apparently nobody has an example of this anywhere; I looked and couldn't find anything and so I am asking here).
For example, how would I implement this helper step action in Java? In this case, how do I get access to the WebDriver driver instance from within Java context? Then, how do I call the embed?
#Slf4j
public class SeleniumStepActions extends StepActions {
public SeleniumStepActions(FeatureContext featureContext, CallContext callContext, Scenario scenario, LogAppender appender) {
super(featureContext, callContext, scenario, appender);
}
#When("^screenshot$")
public void takeAScreenShot()
{
// goals is to simulate this in a karate js test
// * def bytes = driver.screenshot()
// * karate.embed(bytes, 'image/png')
log.info("Testing my own custom action.");
}
}
It is possible the above won't work. I am just looking to be pointed in the right direction by someone who knows. I wish there was such an example in the karate demo.
You can't. Which is why there ain't any demo :P
For a detailed discussion, please read this thread: https://github.com/intuit/karate/issues/398
The summary:
Karate does not support the "Step Definitions" that Cucumber does
There are 2 ways to inject custom logic, a) JS, b) Java interop
These are more than sufficient to implement something close to custom "keywords" - see this example: https://twitter.com/KarateDSL/status/1128170638223364097
and another: https://twitter.com/KarateDSL/status/1144458169822806016
If you insist on making your test read like "plain english" (which IMHO is not worth it) - then Karate may not be the best choice for your team.
I am supposed to migrate on Cucumber. I do have project framework with Selenium, TestNG with Data Driven Framework, Maven. I am exploring Cucumber feasibility with TestNG annotation.
My question is, How we can create connection between #Test method and Step definition of cucumber. Let's example our code is written in #BeforeClass, #Test, #AfterClass method. So how we can migrate with Step definition.
Feature File :
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday
Given today is Sunday
When I ask whether it's Friday yet
Step Definition:
#Given("^today is Sunday$")
public void today_is_Sunday() {
// Write code here that turns the phrase above into concrete actions
System.out.println("this is demo1");
}
#When("^I ask whether it's Friday yet$")
public void i_ask_whether_is_s_Friday_yet() {
// Write code here that turns the phrase above into concrete actions
System.out.println("this is demo2");
}
Class Exection:
#CucumberOptions(features = "cfeature/firstDemo.feature", glue = { "mytest/Stepd" })
public class demo01 extends AbstractTestNGCucumberTests {
private TestNGCucumberRunner tcr;
#BeforeClass(alwaysRun = true)
public void beforeClass() throws Exception {
tcr = new TestNGCucumberRunner(this.getClass());
}
#Test(groups="cucumber", description="Runs CucumberFeature")
public void testdemo() {
System.out.println("Hello");
}
#AfterClass(alwaysRun = true)
public void afterClass() {
tcr.finish();
}
}
Console:
Hello
[33mUndefined scenarios:[0m
[33mcfeature/firstDemo.feature:4 [0m# Sunday isn't Friday
1 Scenarios ([33m1 undefined[0m)
5 Steps ([33m5 undefined[0m)
0m0.073s
You can implement missing steps with the snippets below:
As of now, #Test annotation is calling. But, How to replace it with Step Definition. Please assist.
Not sure what the confusion here. Here's how you can relate TestNG and cucumber terminologies.
<test> tag in TestNG can be visualized as a feature file in cucumber.
#Test method in TestNG can be visualized as a scenario in cucumber.
A Step definition in cucumber has nothing directly equivalent to in TestNG because, its part of a scenario. But for the sake of understanding you can visualize it as one line of code doing a logical operation in TestNG.
The default implementation of AbstractTestNGCucumberTests is as below:
It contains a data provider internally which provides one feature file at a time.
It contains a #Test method which is bound to the above mentioned data provider, which retrieves all the scenarios in the feature file and then runs them one after the other.
You can build your own variant of AbstractTestNGCucumberTests to do various different things (such as support concurrent scenario execution which is currently not available in Cucumber JVM bindings).
As an example you can take a look at Cucumber-roadrunner library that I built which uses the above concept to support parallel scenario execution and also provides thread safe reports.
With respect to the error you are facing viz., You can implement missing steps with the snippets below: is basically because cucumber jvm bindings perhaps isn't able to bind your feature file with a glue code (which is what you are providing via the #CucumberOptions annotation). You should perhaps take a closer look at the cucumber jvm bindings documentation to understand how to provide the correct values.
You can also take a look to gherkin with QAF which is pure TestNG implementation for gherkin. It is using TestNG (NOT cucumber runner) and provides you all the features of testNG including parallel execution, listeners, grouping, priority etc...
Each scenario converted as TestNG test and you can run scenarios as parallel. Furthermore you can also use inbuilt or custom data-providers while authoring BDD. No need additional runner just configure as usual using appropriate factory class for the BDD syntax you are using.
I have a very simple package structure, only one level deep for all my grails artifacts- the name of the application "estra"- because the grails application structure is already providing the separation folders. But when writing unit-tests all the classes are inside the same estra.* package and I want to keep them separated like this estra.domain, estra.controllers, etc.
Right now everything works fine, but the tests are pretty simple. Will I face any problem in the future with dependency injection or something?
No, the package name don't influence in your test since in your test class you "say" which class is tested, using the #TestFor annotation. But remember that in unit tests you need to manually set your dependencies.
class ServiceOne {
def serviceTwo
}
#TestFor(ServiceOne)
class ServiceOneTests {
#Before
public void setup() {
service.serviceTwo = new ServiceTwo() //or mocked instance...
}
}
I'm currently extending Degraph to check for violation of dependency rules.
So far I have created matchers for ScalaTest, since this is my favorite test library for the JVM. But many others prefer or have to use other libraries. So I'd like to provide matchers (or whatever is the appropriate equivalent) for those as well.
On the other hand I'm lazy so I don't want to provide a matcher for every conceivable library.
So the question is:
Is there some kind of matcher library that can be used in all (or the most / many) test libraries? Or maybe a wrapper that converts Hamcrest matchers to matchers for all the other libs?
The libraries I would like to support (in that order of importance):
JUnit
ScalaTest
most popular testing library for Groovy
most popular testing library for Clojure
Specs2
TestNG
Every unit testing framework I've seen will fail a test that throws an exception.
Hamcrest's assertThat(T value, Matcher<T> matcher) will throw an AssertionError if matcher.matches(value) returns false.
Therefore, Hamcrest should work out-of-the-box with most testing frameworks. For other libraries, such as for building mock objects, you'll need to either use Hamcrest's Matcher objects natively or write an integration layer. One way would be to create a method to decorate any matcher, for example*,
ArgumentMatcher<T> decorate(final Matcher<T> matcher) {
return new ArgumentMatcher() {
public boolean <T> accepts(T value) {
return matcher.matches(value);
}
}
}
* This is a contrived example using a theoretical Mocking API.