4

Is it possible to have a common @Before and @After fixtures that can be used across multiple test classes?

I have segregated the tests (into classes) based on modules (Inventory, Sales, Purchase etc.). For all these tests, user Login is a prerequisite, currently I am having it in @Before for each class. The problem is when I need to change user id or password, I need to change in every class. Is there a way to write the @Before / @After that can be used in all the test classes? Does testsuite come handy by any means in this case?

3
  • 6
    Did you try putting them in an abstract parent class? Commented Jun 25, 2014 at 8:34
  • 1
    Plenty of good answer to the question, but I would suggest that if you need to supply login information in many places then you may want to look at your class structure. Test one class that handles logging in and mock that class everywhere that logging in is required Commented Jun 25, 2014 at 8:41
  • mocking is a good idea indeed. However the approach I am looking for is of more of generic, while login is an example. Some how I couldn't find right link for mocking. Can you provide an example or link @Xetius ? Commented Jun 25, 2014 at 8:54

4 Answers 4

8

The @Before and @After are applicable to inheritance:

public abstract class AbstractTestCase {

    @Before
    public void setUp() {
        // do common stuff
    }
}

If you want to do specific stuff in each test case you can override it:

public class ConcreteTestCase extends AbstractTestCase {

    @Before
    @Override
    public void setUp() {
        super.setUp();
        // do specific stuff
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

marking this as answer though steve has provided the answer first - as this extends the approach with overriding - thanks Andre
5

You can create a @ClassRule for your test suite. It is invoked for each test. See API and Example for ExternalResource on how to apply before/after.

Comments

2

you can use @Before annotations in an abstract parent class like so:

public class TestMe extends TestParent {
    @Test
    public void test() {
        System.out.println("Hi, here is a test. The username is: " + getUsername());
    }
}

with parent class:

public abstract class TestParent {
    private String username;

    @Before
    public void setUp() {
        username = "fred";
    }

    public String getUsername() {
        return username;
    }
}

Comments

1

I know 2 solutions:

  1. Use abstract base class as it is mentioned in comment by @vikingsteve.
  2. Use Rules. You can implement your custom rule that does what you need and then add it to each test case you need.

    public class MyTest {
        @Rule Rule myBeforeAfterRule = new MyTestLifecycleRule();
    
        // your code
    }
    

The rule-based solution IMHO is more flexible because inheritance is not always applicable for all use-cases. Moreover you can combine several rules in one test case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.