How to exclude tests in Geb? - selenium

I am using Geb with Spock and I'd like to introduce tests tagging. It's easily doable in Spock using a syntax:
runner {
if (!System.properties.containsKey('test.include.slow')) {
exclude Slow
}
}
This would exclude all the tests annotated with #Slow if test.include.slow property is not found. Unfortunatelly putting exclude into GebConfig.groovy doesn't really do it, because there's no such method to use there.
Is there a way to use this exclude method with Geb?

Since you're using spock, you can use #IgnoreIf at the class or method level.
Here's a good article on it:
http://mrhaki.blogspot.com/2014/06/spocklight-ignore-specifications-based.html
Since you have a system property for it, you should just be able to plug it right in:
#IgnoreIf({ !System.properties.containsKey("test.include.slow") })

You're on the right track but you should put that runner configuration code in SpockConfig.groovy and not GebConfig.groovy as it is Spock specific configuration.

Related

How to intercept JUnit5 method annotated with #Disabled?

I would like to write a JUnit5 Extension where I have to take some action when a test method annotated with #Disabled is found. Unfortunately, beforeTestExecution() is not called for such methods. Does anybody have an idea how to intercept such #Disabled test methods ?
Thanks !
As described in the User Guide, you can disable the built-in ExecutionCondition that handles #Disabled by default by setting junit.jupiter.conditions.deactivate to org.junit.*DisabledCondition (see Configuration Parameters on how to set it). This will cause your tests to be executed.
Next, you need to implement your own ExecutionCondition extension, check for #Disabled, take your action and return ConditionEvaluationResult.disabled("...").
In order to avoid having to register your extension on each test class, you can activate Automatic Extension Registration and register your extension globally.
Depending on what you want to achieve, it may be easier to register your own TestExecutionListener (see Plugging in Your Own Test Execution Listeners) and implement executionSkipped().
The extension point used here is https://github.com/junit-team/junit5/blob/master/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/ExecutionCondition.java
It might be interesting to find out how several implementors compose, i.e. if the 2nd one will be called at all and under what circumstances. You’ll probably have to dive into Jupiter code or do some experiments.

Mule- Behaviour-Driven Development using JBehave

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.

testNG : find which test classes will run before any of them are

I am working with testNG where I run an external test framework, receive the result data and assert it. To run the external test framework I need to set up a specification for which tests that should be run. To generate this specification I need to know which tests that are selected in the testNG .xml file.
The only way I could think of doing this is to parse the file manually. But I am hoping for a better solution than this.
Thanks for any answers!
//Flipbed
Edit:
My colleague found solutions to the problem.
In #Factory and #DataProvider annotated methods it is possible to add a parameter of the type ITestContext. Using the variable of that type, one can use the method .getAllTestMethods().
Create a new class that implements IMethodInterceptor. In this class one can override the method 'intercept'. The method takes a parameter of the type List which is a list of all methods that will be run by testNG.
If someone has any other suggestions feel free to add.
//Flipbed
The solution that we used was to number 2 in my edit. We implemented the IMethodInterceptor and used the methods list as well as the ITestContext to both view what tests will run and modify that list.

Run Cucumber JVM tests manually

I have a bit of a special situation. Basically I have a unit test, annotated with #Test, and inside that test I need to execute a Cucumber JVM test class.
Why? Long story. Something to do with classloaders and RoboGuice, it's not very important but it does impose limits on what I can and cannot do.
Here's the test method:
#Test
public void runCucumberFeature() throws Exception {
Cucumber cucumber = new Cucumber(MyCucumberTest.class);
cucumber.run(new RunNotifier());
}
MyCucumberTest is a class I have created, and annotated like this:
//#RunWith(Cucumber.class)
#Cucumber.Options(format = {"pretty", "html:target/cucumber"}, strict=true)
public class MyCucumberTest {
// Empty, as required by Cucumber JVM
}
Why have I commented out the #RunWith annotation? Because if I don't, the Cucumber test runner will pick up the test and run it, which I don't want because I am running the test manually.
The problem is that the above doesn't work. It looks like Cucumber is finding the feature files, it is verifying that MyCucumberTest contains the #Givens etc, it even prints out the test as if it was running it.
But it doesn't. No code is executing inside the #Given, #When and #Then methods. I'm not sure why this is, but I have a vague idea that the Cucumber JVM test runner doesn't want to execute the code because the class isn't annotated with #RunWith.
Can anyone help?
I can't provide the solution you're looking for, but....
... have you considered tagging the test that you want to run manually (e.g. with #Manual)?
Then you could uncomment your #RunWith annototation and exclude the manual test by adding --tags ~#Manual to your Cucumber-JVM invocation.
In your manual JUnit invocation you could add --tags #Manual

How to test a grails Criteria query?

first post here and hopefully relevant to many people.
I'm working on writing integration tests for a domain and on that domain I have a query using the withCriteria() method. I've searched all over the net and found many that give you detailed instructions on how to go about mocking a criteria query, but none on how to exactly test it.
I've tried mocking the domain using the mockDomain(domain,list) function, and setting up a domain for the test to use in the setUp() then calling the criteria and I get nothing.
I did a similar findBy here and got the results, but not exactly the ones I was looking for. I'm pretty sure it's not just my query, but the criteria, I've read in a few places criteria does not work in service testing. The query thus far has worked for me in the app, but I want to have some tests that I can refer to later in case my code base changes.
I've actually done as many have suggested and pulled out the code for the query and made it a static method in my domain so that I can mock it for the tests that use it, but now I'm stuck with how to actually test this part. Do I need to run the app and just do functional testing from that standpoint, or is there some way I could do this in the grails unit/integration testing. I'll post my query below.
static Attribute getDefinitionsUsingCriteria(List categoryNames, List types){
def definitions = Definition.withCriteria() {
and {
'in'('type', types)
if (categoryNames) {
categories {
'in'('name', categoryNames)
}
}
}
}
return definitions
}
Definitions has a string property type, and has a property categories of type Set that each element in this set has a String name property.
I'm still pretty new to grails and have been reading many reference books, and I'm surprised this is missing in all of the books I've read thus far. I hope this is something that is just a mistake on my part, and easily testable. I appreciate any help, and thanks for reading this long post.
JR.
One way: move the test from test/unit to test/integration folder. Criteria won't work in unit test (there's no Hibernate there), but will in integration. Never use mockDomain() in integration tests.
Note: don't make the method static - it only complicates testing.
Second way: In unit tests - use mockDomain(). Just rely on the fact that the logic is pretty straightforward, and unit-test everything except the method. Override it in setUp() like:
Definition.metaClass.getDefinitionsUsingCriteria = { List categoryNames, List types ->
delegate.findAll{ (it.type in types) &&
(it.categories.find { c -> c in categoryNames })
}
}
Grails 2.0.1 now has native #Mock for test criteria, but groupProperty is not implemented yet.
I wrote mock criteria plugin (with groupProperty)
https://github.com/fabiooshiro/plastic-criteria
it works in 1.3.7
Criteria are supported since grails 2.2. See "Unit Testing GORM" at What's new in Grails 2.2