I have a test suite where I am logging out of the system in @After
and closing the browser in @AfterClass
. I am trying to use @Rule
to take failed test screenshot using Selenium for every test method. I checked manually that @Rule
only runs before every @Before
but I want to set it up after @Test
and before @After
. I couldn't find out simple solution. Any help will be appreciated.
public class MorgatgeCalculatorTest {
@Before
public void before(){
System.out.println("I am before");
}
@BeforeClass
public static void beforeclass(){
System.out.println("I am beforeclass");
}
@Test
public void test(){
System.out.println("I am Test");
}
@Test
public void test2(){
System.out.println("I am Test2");
}
@After
public void after(){
System.out.println("I am after");
}
@AfterClass
public static void afterclass(){
System.out.println("I am afterclass");
}
@Rule
ExpensiveExternalResource ExpensiveExternalResource = new ExpensiveExternalResource();
static class ExpensiveExternalResource implements MethodRule {
public ExpensiveExternalResource(){
System.out.println("I am rule");
}
@Override
public Statement apply(Statement arg0, FrameworkMethod arg1, Object arg2) {
// TODO Auto-generated method stub
return null;
}
}
The output I am getting is
I am beforeclass
I am rule
I am before
I am Test
I am after
I am rule
I am before
I am Test2
I am after
I am afterclass
arg0.evaluate()
logging before and after the call. This would show that the rule is run around@Before
/@After
like is also noted in the JavaDoc of JUnit. – Selfreproach