dependsOnMethods for #AfterTest not finding the test method - java

I am trying following code:
public class ShashiTest {
#Test
public void test1(){
System.out.println("1===========");
}
#Test(dependsOnMethods="test1")
public void test2(){
System.out.println("2===========");
}
#Test(dependsOnMethods="test2")
public void test3(){
System.out.println("3===========");
}
#AfterMethod(dependsOnMethods={"test2","test3"})
public void test4(){
System.out.println("4===========");
}
}
I am expecting output as:
1===========
2===========
4===========
3===========
4===========
But I am getting exception as test method not found:
com.ShashiTest.test4() is depending on method public void com.ShashiTest.test2(), which is not annotated with #Test or not included.
at org.testng.internal.MethodHelper.findDependedUponMethods(MethodHelper.java:111)
Where I am making the mistake? How I can achieve my goal?

#AfterMethod declares that this method is run after every method annotated with #Test. Right now you have a conflict with test4() being called after test1() and before test2(), while also requiring it to be run after test2(). Refer to this for more in-depth discussion.
edit: I should probably make the call order more clear.
test1()->test4()
test2()->test4()
test3()->test4()
As you can see, requiring test4() to be run after test2() and test3() is in conflict with the #AfterMethod annotation requiring it be called after every method.

dependsOnMethod is not working like that and just used to order methods between them.
The javadoc is clear enough IMO:
The list of methods this method depends on. There is no guarantee on the order on which the methods depended upon will be run, but you are guaranteed that all these methods will be run before the test method that contains this annotation is run. Furthermore, if any of these methods was not a SUCCESS, this test method will not be run and will be flagged as a SKIP. If some of these methods have been overloaded, all the overloaded versions will be run.
But the exception should not happen so I opened an issue for it.
About your need which is running an #AfterMethod for specific methods only (something looks weird, but why not), you can do the following:
public class ShashiTest {
#Test
public void test1(){
System.out.println("1===========");
}
#Test(dependsOnMethods="test1")
public void test2(){
System.out.println("2===========");
}
#Test(dependsOnMethods="test2")
public void test3(){
System.out.println("3===========");
}
#AfterMethod
public void test4(Method m){
switch(m.getName()) {
case "test2":
case "test3":
System.out.println("4===========");
}
}
}
should work as expected.

Bit late to answer but I just faced this problem today. Error: com.expedia.FlightBooking.tearDown() is depending on method public void com.expedia.FlightBooking.flightBooking(), which is not annotated with #Test or not included.
Solution: Changing dependsOnMethods to dependsOnGroups Ex: #AfterTest(dependsOnGroups = {"flightBooking"}) has solved my problem.

Related

EasyMock verifying calls to mock in tearDown method after verify finished

I am seeing inconsistent behaviour in EasyMock tests that I don't understand.
My first test passes..
public class MockATest {
private final AtomicLong aMock = createStrictMock(AtomicLong.class);
#Before
public void setUp() {
aMock.set(101L);
}
#After
public void tearDown() {
aMock.set(999L);
}
#Test
public void testA() {
reset(aMock);
replay(aMock);
// TODO : test stuff here
verify(aMock);
}
}
.. but my second test fails ...
public class MockBTest {
private final List<Long> bMock = createStrictMock(List.class);
#Before
public void setUp() {
bMock.add(101L);
}
#After
public void tearDown() {
bMock.add(999L);
}
#Test
public void testB() {
reset(bMock);
replay(bMock);
// TODO : test stuff here
verify(bMock);
}
}
The failure reason is
Unexpected method call List.add(999)
I have 2 questions really...
Why is the behaviour different for the 2 tests?
Why is the add(999L) that happens in the tearDown method is being verified after the verification in the testB method has already fully completed?
(I know I can make this work by adding another reset(bMock) in after the verify(bMock) but I am not sure whether this is just avoiding the issue)
Why is the behaviour different for the 2 tests?
Because AtomicLong.set is typed void AtomicLong.set(long) so it's a void method. The recording is fine. However, List.add is typed boolean List.add(E) so it's not a void method. The correct way to record a non-void method is to do expect(list.add(101L)).andReturn(true).
Why is the add(999L) that happens in the tearDown method is being verified after the verification in the testB method has already fully completed?
Because it never goes in testB(). EasyMock throws an error on the call to bMock.add(101L) in setUp() so it goes directly to the tearDown which fail as well and hides to exception from setUp().

Junit test cases stopped executing after 2 test cases

I am writing DBunit (junit of DAO methods) in Spring + Hibernate framework and using Java8.
I have many test cases in the Test class, which when run individually are successful. But when I run that particular class, the test cases stops its execution at the 3rd test case by executing the 2 test cases.
I tried shuffling the test cases, it takes up any method at random, it stops executing at the third test case.
The configuration is implemented properly, in the below snippet, it fails at the testCMethod test case.
public class ABCDAOTest extends Test case {
#Override
#Before
public void setUp() throws Exception {
super.setUp()
}
#Test
public void testAMethod(){
/*
My test case
*/
}
#Test
public void testBMethod(){
/*
My test case
*/
}
#Test
public void testCMethod(){
/*
My test case
*/
}
#Test
public void testDMethod(){
/*
My test case
*/
}
}
Please provide view your input.
To run the tests in a particular order you want it could be done like this
import org.junit.runners.MethodSorters;
import org.junit.FixMethodOrder;
import org.junit.Test;
#FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SampleTest {
#Test
public void firstTest() {
System.out.println("first");
}
#Test
public void secondTest() {
System.out.println("second");
}
}
Remember MethodSorters is an Enum whose other constants are
JVM - Leaves the test methods in the order returned by the JVM. Note that the order from the JVM may vary from run to run.
DEFAULT - Sorts the test methods in a deterministic, but not predictable, order.
and about not running the tests after second one, the probable reason can be the third test run might not be able to give result because of infinite loop.
So we can also restrict the runtime of a test case by adding a Rule and using Timeout, as used following
public class LongRunningTests {
#Rule
public Timeout globalTimeout = Timeout.seconds(5);
#Test
public void whatWeDoInATestMethodEchoesInEternity() {
while(true);
}
}
hope this can work for you.

Marking test methods with #Test(dependsOnMethods=..) testNG annotation make them not to execute

I've got a problem in my production test suite runs.
testng.xml has set up to run test suite in multithreaded environment using custom listener. As result there are several driver instances that are running separately and in parallel, with each test.
Last time suite started failing and I noticed strange behavior:
Each test in each test method which has dependsOnMethods in its #Test annotation do not execute. Driver just skipps them, and does not execute #AfterTest methods as result.
Or, I suppose It does not skip them, it does not report to depend methods that "login" method is done and they can go on and execute.
But i have no idea why is it happening
Smth like this:
#BeforeClass
protected void beforeClassInit(){
setUp(///);
}
#Test
public void login() {
//login activities
}
#Test(dependsOnMethods = "login")
public void createSmth() {
///
}
#Test(dependsOnMethods = "createService")
public void deleteSmth() {
///
}
#AfterClass(alwaysRun = true)
protected void afterClass() {
shutDown();
}
See in your code,
#BeforeClass
protected void beforeClassInit(){
setUp(///);
}
#Test
public void login() {
//login activities
}
#Test(dependsOnMethods = "login", alwaysRun=true)
public void createSmth() {
///
}
#Test(dependsOnMethods = "createSmth", alwaysRun=true)
public void deleteSmth() {
///
}
#AfterClass(alwaysRun = true)
protected void afterClass() {
shutDown();
}
createSmth and deleteSmth always run even if dependant method will get fail to execute. Before and after class annotation will be run before/after the first/last test method in the current class is invoked.
#BeforeClass when multiple tests need to share the same computationally expensive setup code. #BeforeClass will be executed only once.
It works even if you will run using testng.xml in parellel
The problem was in testng logic.
Through tons of experiments it was defined, that TestNG always runs dependent methods in the end of parallel run.
Means, i.e. you have 3 Test Classes:
Test1.java
Test2.java
Test3.java
and each has some test methods.
TestNG suite contains that 3 classes will run each non-dependent method from those classes, than come back and finish run of those dependent methods which left.
Crazy behavior, but looks that's it/

How to add Grouping and Dependency in #BeforeMethod On TestNG

Is there any way to give dependency to #BeforeMethod on #Test,because in my scenario different TestMethod have different setup and I need one setup depends on TestMethod. I add here some code snippet for better understanding
#BeforeMethod(groups = {"gp2"})
public void setUp1() {
System.out.println("SetUp 1 is done");
}
#BeforeMethod(groups = {"gp1"}, dependsOnGroups = {"tgp1"})
public void setUp2() {
System.out.println("SetUp 2 is done");
}
#Test(timeOut = 1, groups = {"tgp1"})
public void testMethod() throws InterruptedException {
Thread.sleep(2000);
System.out.println("TestMethod() From Group1");
}
#Test(dependsOnMethods = {"testMethod"}, groups = {"tgp2"})
public void anotherTestMethod() {
System.out.println("AnotherTestMethod()From Group1 and Group2");
}
OutPut
SetUp 1 is done
SetUp 2 is done
but I need setUp1() should be executed not setUp2() because it is depends on tgp1 group.
Another thing I observe that,If I change dependencies from
#BeforeMethod(groups = {"gp1"}, dependsOnGroups = {"tgp1"})
to
#BeforeMethod(groups = {"gp1"}, dependsOnMethods = {"testMethod"})
then I got an Exception like
setUp2() is depending on method public void testMethod() throws java.lang.InterruptedException, which is not annotated with #Test or not included.
I need execution should be on this steps
SetUp1---->testMethod1()------->SetUp2--------->testMethod2()
I need this because different TestMethods have different work and it have different SetUp().
In your case, you can simply call respective setUp method from respective Test method as you really don't have common #BeforeMethod for each Test.
Or you can separate these two tests into different Test classes each having a #BeforeMethod.
Or you can do conditional setup call based on test method name with one #BeforeMethod method as below. Here #BeforeMethod can declare a parameter of type java.lang.reflect.Method. This parameter will receive the test method that will be called once this #BeforeMethod finishes
#BeforeMethod
public void setUp(Method method) {
if (method.getName().equals("testMethod")) {
setUp1();
} else if (method.getName().equals("anotherTestMethod")) {
setUp2();
}
}
public void setUp2() {
System.out.println("SetUp 2 is done");
}
public void setUp1() {
System.out.println("SetUp 1 is done");
}
The purpose of the #BeforeMethod is to run before each test method annotated with #Test and do some set-up work. Therefore it is somewhat confusing why you would want to create dependency that works the other way - execute the test method before set-up method.
The usual approach would be following:
#BeforeMethod
public void setUp() {
}
#Test
public void testMethod1() {
}
#Test
public void testMethod2() {
}
Which most likely generate following execution list:
setUp()
testMethod1()
setUp()
testMethod2()
If there multiple #BeforeMethods all of them will be run before the #Test method.
Now if you want to run different groups, you would annotate different methods with different groups and specify, which groups to run. However, for that you will need to provide either testng.xml or specify which groups to execute.
EDIT (based on additional comment):
I would suggest one of the following approaches:
Separate test methods into different classes and have #BeforeMethod
in each class perform required setup. This especially makes sense if
the tests are covering different areas.
Define different groups and
put corresponding test and set-up methods into same group. This
option lets you mix and match set-up methods with test method.
Some things that are unclear from your question, but if relevant could benefit from suggested approaches:
Is there need for dependency?
Does the order or the tests matter?

How to run tearDown type method for a specific test in JUnit class with multiple tests?

I have a junit testCase class with multiple test methods in it ( As requirement , we don't want to create separate class for each test.)
I wanna create a tearDown type method for EACH test method , which will run specifically for that test. Not for ALL test.
My problem is , in many tests i Insert record in database, test it and delete it after test.
But, If a test fails mid way , control don't reaches till end my dummy record ain't deleting.
I think only ONE tearDown() is allowed for one class, and this tearDown() don't know what object/record i created or inserted and what to delete!!!
I want to create a tearDown() or #After method just for one specific test. Something like finally{} in java for each method.
For Eg:
public class TestDummy extends TestCase {
public void testSample1(){
InsertSomeData1();
assertFalse(true);
runTearDown1();
}
public void testSample2(){
InsertSomeData2();
assertFalse(true);
runTearDown2();
}
public void runTearDown1(){
deleteDummyDatafromTestSample1....
}
public void runTearDown2(){
deleteDummyDatafromTestSample2....
}
}
Here control will never go to runTearDown1() or runTearDown2() and I don't a one common tearDown() because it won't know what data I inserted and thats specific to each method.
It seems your test relies on a fixed database, and future tests will break if your current test breaks. What I'd recommend is not to focus on this particular problem (a test-specific tearDown method that runs for each test), but your main problem - borken tests. Before your test run, it should always work with a clean database, and this should be the case for each test. Right now, your first test has a relationship with the second (through the database).
What the right approach would be is that you recreate your database before each test, or at the very least reset it to a basic state. In this case, you'll want a test like this:
public class TestDummy {
// this code runs (once) when this test class is run.
#BeforeClass
public void setupDatabase() {
// code that creates the database schema
}
// this code runs after all tests in this class are run.
#AfterClass
public void teardownDatabase() {
// code that deletes your database, leaving no trace whatsoever.
}
// This code runs before each test case. Use it to, for example, purge the
// database and fill it with default data.
#Before
public void before() {
}
// You can use this method to delete all test data inserted by a test method too.
#After
public void after() {
}
// now for the tests themselves, we should be able to assume the database will
// always be in the correct state, independent from the previous or next test cases.
#Test
public void TestSample2() {
insertSomeData();
assertTrue(someData, isValid());
}
}
Disclaimer: JUnit 4 tests (using annotations), might not be the right annotations, might not even be the right answer(s).
You could have smth like this:
interface DBTest {
void setUpDB();
void test();
void tearDownDB();
}
class DBTestRunner {
void runTest(DBTest test) throws Exception {
test.setUpDB();
try {
test.test();
} finally {
test.tearDownDB();
}
}
}
public void test48() throws Exception {
new DBTestRunner().runTest(new DBTest() {
public void setUpDB() {...}
public void test() {...}
public void tearDownDB() {...}
});
}
#iluxa . Gr8.. Your solution is perfect!!! In one test class i created two tests test48 and test49 (same as required in my code above testSample1 and testSample2) and viola! every test method now gets its own setup() and tearDown. Only this solution looks little complicated as need to use DBTestRunner in each method, but I don't see any better solution. I was thinking Junit may have some direct solution. like #After or tearDown() with some parameter or something.
Tks a lot.
Use MethodRule:
public class MyRule implements MethodRule {
#Override
public Statement apply(final Statement base, FrameworkMethod method, Object target) {
return new Statement() {
#Override
public void evaluate() throws Throwable {
try {
base.evaluate();
} catch (AssertionError e) {
doFail();
} finally {
doAnyway();
}
}
};
}
}
Then declare it in your test class:
public class TestDummy{
public MethodRule rule = new MyRule();
......
}

Categories