Run method when (Cucumber) Annotation is called - java

Is there a way in JAVA to listen for a specific annotation that is being called, and run a method before / after the annotation, specifically in a Cucumber / JUnit framework? For example, when the following code is being called due to step in your feature file ("Then something happens"):
#Then("something happens")
public void somethingHappens(){
// run code in method
}
That before / after the somethingHappens() method is called, another method is called like:
public void doSomethingElseBeforeAnnotation(){
// run code in method
}
There is the #BeforeStep and #AfterStep in the Cucumber framework, but i want to be able to run the method only at, for example, the "#Then" annotations. So actually, i neeed something like a #BeforeThen :)

Related

invoking a test method each time before other test methods

I have a test method as below that two other test methods are dependent to this method and this method should run before these two each time and not only once for both.
#Test(dataProvider = "requestParameterProvider", groups = "jsonRequest")
public void saveNewActivity_correctValues(Service service,
Map<String, Object> requestMap){}
#Test(dependsOnMethods = "saveNewActivity_trackRequest_correctValues", dataProvider = "responseParameterProvider")
public void commitActivity_correctValues(Service service){}
#Test(dependsOnMethods = "saveNewActivity_trackRequest_correctValues", dataProvider = "exceptionParameterProvider")
public void failActivity_correctValues(Service service, FailureReason failureReason){}
what happens at above case is saveNewActivity_correctValues method run once first and then two other method run after that. but i want first method to be invoked two times before each dependent method and once as separate test. i can't put first method as #BeforeMethod because it is already a test and have a provider of it's own.
Use the #Before annotation on the method you want to run before all tests.
If you don't want it to run before all methods, but only some, either refactor your tests out into 2 classes and use #Before in one, and not in the other and move your methods appropriately.
The other option is to just call the method(s) from each test you want them to run before.
I'm assuming you are using JUnit - so see here:
http://junit.sourceforge.net/javadoc/org/junit/Before.html
If not, update your post with what you are using.

Overriden BeforeClass method is stopping entire test execution

I am using TestNG with Selenium WebDriver, the framework that I use has one base class which has BeforeClass method and all the Suite classes extend from this base class and have overriden BeforeClass methods as shown.
public BaseClass{
#BeforeClass
public void preConditions{
//primary actions like opening browser and setting preferences
}
}
public TestSuiteClass extends BaseClass{
#BeforeClass
#Override
public void preConditions(){
super.preCnditions();
//specific preconditions required by test suite
}
}
The problem I am have is, if an overridden method is failing for any reason, all the test suits/cases gets skipped and entire test execution stops.
How to stop this from happening?
If something fails in #Before... annotated procedure the tests will be skipped instead of failing, because the problem is not with your test cases, but with the process before them. So it looks like your car cannot cross the river on a broken bridge, but it's not your car's fault.
If you really want to do some hacking about it, you can find ideas here!
You can find a way around this, but you need to consider if you realy want to do it. The idea of the method with #BeforeClass annotation is to set relevant data for the test (you even called it preConditions). If it fails, the test won't pass anyway.
One way to do it is to remove the #BeforeClass annotation and call the method from the test itself
public TestSuiteClass etends BaseClass {
public void customPreConditions() {
super.preCnditions();
}
#Test
publuc void someTest() {
customPreConditions();
// do the test
}
}
The test will continue even if customPreConditions() is not successful.

How does all annotations work in TestNg without main() method

I have a doubt in TestNG with Java. I am completly new to TestNG. My doubt is, How all the test cases are executing using TestNG in java without having main() method? Please suggest me if you have any ideas. Following code is the example of a sample test case using TestNG in java. But if you notice, you can find one thing that there is no main() method in the code. Then, how does the testcases are executing?
I have another doubt. Is main() method needed for selenium Webdriver and TestNG combination to execute a script? Or can we execute testcases without main() method? If we can execute testcases without main(), then how does it is possible?
package com.first.example;
import org.testng.annotations.Test;
public class demoOne {
#Test
public void firstTestCase()
{
System.out.println("im in first test case from demoOne Class");
}
#Test
public void secondTestCase()
{
System.out.println("im in second test case from demoOne Class");
}
}
This is a valid doubt many testers have. Because the main() method is needed to run the Java program and while writing tests in TestNg we don't use main() method, and we use Annotations instead.
Annotations in TestNG are lines of code that can control how the method below them will be executed. So, in short you don't need to write main() method, TestNg do that by itself. Refer the code at the end in Annotations documentation to get the idea how it happens.
As rightly pointed out in this answer: https://stackoverflow.com/a/1918154/3619412
Annotations are meta-meta-objects which can be used to describe other
meta-objects. Meta-objects are classes, fields and methods. Asking an
object for its meta-object (e.g. anObj.getClass() ) is called
introspection. The introspection can go further and we can ask a
meta-object what are its annotations (e.g. aClass.getAnnotations).
Introspection and annotations belong to what is called reflection and
meta-programming.
Also, it's not necessary to have main() method in your tests, but you can use main() method to run the TestNg tests if you want. Refer this.
to run script from cmd prompt we use below statement,
java org.testng.TestNG testng1.xml
main method in TestNG.java class how accept the command line argument,
public static void main(String[] argv) {
TestNG testng = privateMain(argv, null);
System.exit(testng.getStatus());
}
You saw it right. Test-cases get executed through testng, the testing framework which was inspired from junit without having the main() method but extensively uses annotations.
Annotations
As per the documentation in Annotations majority of the APIs require a huge amount of boilerplate code. To write a web service you need to provide a paired interface and implementation. This boilerplate could be automatically generated by a tool if the program can be decorated with annotations indicating which methods were remotely accessible. Annotations doesn't affects the program semantics directly but they do affect the way programs are treated by tools and libraries, which can in turn affect the semantics of the running program.
TestNG
TestNG is a simple annotation-based test framework which uses a marker annotation type to indicate that a method is a test method and should be run by the testing tool. As an example:
import org.testng.annotations.Test;
#Test
public void foo() {
System.out.println("With in foo test");
}
The testing tool which is being used is as follows:
import java.lang.reflect.*;
public class RunTests {
public static void main(String[] args) throws Exception {
int passed = 0, failed = 0;
for (Method m : Class.forName(args[0]).getMethods()) {
if (m.isAnnotationPresent(Test.class)) {
try {
m.invoke(null);
passed++;
} catch (Throwable ex) {
System.out.printf("Test %s failed: %s %n", m, ex.getCause());
failed++;
}
}
}
System.out.printf("Passed: %d, Failed %d%n", passed, failed);
}
}

Invoke a specific JUnit test programmatically

I would like to run a specific JUnit test method programmatically. I know I can run the whole test using the following code snippet:
Class<?> test = Class.forName("MyTestClass");
JUnitCore junit = new JUnitCore();
Result result = junit.run(test);
However, I would like to run a specific method within this test class which contains multiple methods.
It would also be fantastic if I could control the setUp/tearDown behaviour.
Thank you
There's an overloaded JUnitCore#run method version that accepts Request. While Request has Request#method factory method to:
Create a Request that, when processed, will run a single test. This is
done by filtering out all other tests. This method is used to support
rerunning single tests.
If you need to control #setUp / #tearDown (i.e., methods marked with #Before and/or #After annotations) you can extend the class and override methods you need to alter.

How can I make JUnit 4.8 run code after a failed test, but before any #After methods?

I'm driving a suite of Selenium tests (actually WebDriver-backed Selenium) using JUnit 4.8.2. I'd like the tests to automatically take a screenshot of the browser as soon as the test fails an assertion. All the tests inherit from SeleniumBaseTestCase, and the majority then further inherit from from SeleniumBastTestCaseWithCompany (which uses #Before and #After methods to create and then clean up common test data via Selenium).
I've tried adding a subclass of TestWatchman as a #Rule in SeleniumBaseTestCase, overriding TestWatchman's failed method to take the screenshot. The trouble is that the #After methods cleaning up the test data are being run before TestWatchman's failed method is called, so the screenshots are all of the final step of the clean-up, not the test that failed.
Looking into it a little, it seems that TestWatchman's apply method just calls the passed Statement's evaluate method (the only exposed method), which calls the #After methods, leaving TestWatchman (or any other Rule) no chance to insert any code between the execution of the test and of the #After methods, as far as I can tell.
I've also seen approaches that create a custom Runner to alter the Statements created so that methods annotated with the custom #AfterFailure are run before #After methods (so the screenshot can be taken in such an #AfterFailure method), but this relies on overriding BlockJUnit4ClassRunner's withAfters method, which is deprecated and due to become private, according to the documentation, which suggests using Rules instead.
I've found another answer on SO about the #Rule lifecycle that makes it sound like this simply might not be possible in JUnit 4.8, but may be possible in JUnit 4.10. If that's correct then fair enough, I'd just like confirmation of that first.
Any thoughts on an elegant and future-proof way in which I can achieve what I want would be much appreciated!
You are right in your analysis, #Befores and #Afters are added to the list of Statements before any Rules. The #Before gets executed after the #Rule and the #After gets executed before the #Rule. How you fix this depends on how flexible you can be with SeleniumBaseTestCaseWithCompany.
The easiest way would be to remove your #Before/#After methods and replace them with an ExternalResource. This could look something like:
public class BeforeAfterTest {
#Rule public TestRule rule = new ExternalResource() {
protected void before() throws Throwable { System.out.println("externalResource before"); }
protected void after() { System.out.println("externalResource after"); }
};
#Test public void testHere() { System.out.println("testHere"); }
}
this gives:
externalResource before
testHere
externalResource after
This field can be put into your base class, so it gets inherited/overridden. Your problem with ordering between #After and your rules then goes away, because you can order your rules how you like, using #RuleChain (in 4.10, not 4.8).
If you can't change SeleniumBaseTestCaseWithCompany, then you can extend BlockJUnit4ClassRunner, but don't override withAfters, but override BlockJUnit4ClassRunner#methodBlock(). You can then call super.methodBlock, and reorder the Statements as necessary[*].
[*]You could just copy the code, and reorder the lines, but withRules is private and therefore not callable from a subclass.

Categories

Resources