Comparing result of one JUnit @Test with another @Test in same class

Go To StackoverFlow.com

0

Is it possible to compare the result / output of one JUnit test to another test in same class?

Below is the algorithm of

Public class CompareResult {

@Before
{
open driver
}

@After
{
quit driver
}

@Test
{
Connect to 1st website
Enter data and calculate value
Store the value in Variable A
}


@Test
{
Connect to 2nd website
Enter data and calculate value
Store the value in Variable B
}

@Test
{
Compare A and B
}
}

When I display the value of variable A & B in 3rd @Test, it is NULL. Can we not use variable in one @Test to another @Test in JUnit? Please advise, I am new to JUnit.

2012-04-04 21:12
by Abdul Hameed


2

Why do they need to be two tests? If you are comparing the values, you really have one test with multiple methods and possibly multiple asserts. And if there aren't any asserts in helper1 and helper2, this becomes even more apparent. A test without an assert is just testing it doesn't blow up!

private helper1
{
    // Connect to 1st website
    // Enter data and calculate value
    // Store the value in Variable A
}


private helper2
{
    // Connect to 2nd website
    // Enter data and calculate value
    // Store the value in Variable B
}

@Test actualTest
{
    // Compare A and B with assertion
}
2012-04-09 00:33
by Jeanne Boyarsky
I have tried with one Test annotation and three methods, but only method which is next to Test annotation is getting executed. Please advise - Abdul Hameed 2012-04-09 13:46
Right. You need to call the other two methods from the method with the test annotation. Just like in regular Java - Jeanne Boyarsky 2012-04-09 23:53
Thanx, it worked - Abdul Hameed 2012-04-10 20:22
Great. You should mark one of our answers as correct to close this out - Jeanne Boyarsky 2012-04-10 20:48


0

You store your values in local variables as I understood. Declare private fields A and B first and then use it to store your data.

Public class CompareResult {

    private String a = null;
    private String b = null;

    @Before
    public void Setup() {
        open driver
    }
    ...

Btw your tests should be independent and passing values from one test to another is not a good way to implement them. Also I didn't work with junit a lot so I don't know how execution order for your tests is set. You should define some tests dependency or something like that and I repeat it once again: this is not correct for tests.

2012-04-05 12:43
by Aleh Douhi
More specifically in JUnit, those variables should be static and have setters and getter - Pavel Janicek 2012-04-05 13:48
Execution order is declared to be changeable between versions of JUnit. It usually isn't but it could be. More importantly, JUnit is free to create new instances for each test which means storing in a local variable wouldn't work. Static would, but is still bad design - Jeanne Boyarsky 2012-04-09 00:31
Thanq all. Defined as Static variable and it worked - Abdul Hameed 2012-04-09 13:43
Ads