3

hello every one i'm using Junit 5 Jupiter and i want to know after each test if it succeeded or fails and use the @AfterEach methods by each test result like if succeeded i will do something but if i fail i will do something else can u help me out .. seems that @rule work for Junit 4 not for Jupiter

package Example;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;



public class SSJunit5 {
    static WebDriver driver;
    @BeforeAll
    public static void setUpBeforeClass() throws Exception {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\david\\Desktop\\Documents\\Chrome Driver\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    @AfterAll
    public static void tearDownAfterClass() throws Exception {
        driver.quit();
    }

    @BeforeEach
    public void setUp() throws Exception {
    }

    @AfterEach
    public void tearDown() throws Exception {
    }

    @Rule
    public TestRule watcher = new TestWatcher() {
        @Override
        protected void succeeded(Description description) {
            System.out.println("Pass!");
        }

        @Override
        protected void failed(Throwable e, Description description) {
            ScreenShots.ScreenShot(driver, "SSafterSendKeys");
            System.out.println("Fail!");
        }
    };
    @Test
    public void test() {
        driver.get("https://www.google.com");
        //ScreenShots.ScreenShot(driver, "webPageON");
        driver.findElement(By.id("lsta-ib")).sendKeys("hello");
        String expected = "asd";
        String actual = "qwert";
        assertEquals(expected, actual);
    }

}
  • 1
    There's no replacement for `TestWatcher`, yet (it's currently being worked on in https://github.com/junit-team/junit5/pull/1393). You could write your own extension and check for `ExtensionContext.getExecutionException()`, though. – Marc Philipp Sep 16 '18 at 13:53
  • There's also a JUnit5 extension equivalent to the JUnit4 `TestWatcher` in the `junit-extensions` library. See [docs](https://github.com/glytching/junit-extensions/blob/master/docs/watcher.md) and [code](https://github.com/glytching/junit-extensions). – glytching Sep 16 '18 at 16:39
  • TestWatcher was introduced to Junit 5.4.0 some days ago. Look at https://stackoverflow.com/a/54690217/5360885 – Alexis Feb 14 '19 at 12:20
  • Have a look on this : (https://stackoverflow.com/questions/51012335/how-to-replace-rule-annotation-in-junit-5 "Rule annotation in JUnit5" :) – MariDubois Jan 03 '20 at 04:14

0 Answers0