Best junit code snippet using org.junit.rules.TestName.starting
Source:ETexasTestWatcher.java  
...33    protected void failed(Throwable e, Description d) {34    }35    /*36     * (non-Javadoc)37     * @see org.junit.rules.TestWatcher#starting(org.junit.runner.Description)38     * Overrides JUnit's TestWatcher starting() method; called when a test39     * starts.40     */41    @Override42    protected void starting(Description description) {43        logStartTime(description);44    }45    /*46     * (non-Javadoc)47     * @see org.junit.rules.TestWatcher#finished(org.junit.runner.Description)48     * Overrides JUnit's TestWatcher finished() method; called when a test49     * finishes.50     */51    @Override52    protected void finished(Description description) {53        logEndTime(description);54    }55    /**56     * Logs the start time of a test.57     *58     * @param description the Description of the test59     */60    public void logStartTime(Description description) {61        String testName = getTestName(description);62        loggedSleepTimeAtStart = BasicWebDriverManager.get().getCurrentLoggedTime();63        printElapsedTime(testName + " : " + description.getMethodName() + " starting");64    }65    /**66     * Logs the end time of a test.67     *68     * @param description the Description of the test69     */70    public void logEndTime(Description description) {71        String testName = getTestName(description);72        long elapsedSleepTime = BasicWebDriverManager.get().getCurrentLoggedTime() - loggedSleepTimeAtStart;73        String sleepTime = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(elapsedSleepTime),74                TimeUnit.MILLISECONDS.toMinutes(elapsedSleepTime) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(elapsedSleepTime)),75                TimeUnit.MILLISECONDS.toSeconds(elapsedSleepTime) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(elapsedSleepTime)));76        System.out.println("Time slept during " + description.getMethodName() + ": " + sleepTime);77        printElapsedTime(testName + " : " + description.getMethodName() + " ending");...Source:ChronicleQueueTestBase.java  
...46    @NotNull47    @Rule48    public TestRule watcher = new TestWatcher() {49        @Override50        protected void starting(@NotNull Description description) {51            if (TRACE_TEST_EXECUTION) {52                Jvm.debug().on(getClass(), "Starting test: "53                        + description.getClassName() + "."54                        + description.getMethodName()55                );56            }57        }58    };59    // *************************************************************************60    //61    // *************************************************************************62    static AtomicLong counter = new AtomicLong();63    @BeforeClass64    public static void synchronousFileTruncating() {...Source:CalacRuleTest.java  
...39  public ExpectedException expectedException = ExpectedException.none();40  @Rule //ãã°åºå ãã¹ãã¦ã©ããã£ã¼41  public TestWatcher testWatcher = new TestWatcher () {42    @Override43    protected void starting(Description description) {44      Logger.getAnonymousLogger().info("start:"45      + description.getMethodName());46    }47    @Override48    protected void succeeded(Description description) {49      Logger.getAnonymousLogger().info("succeeded:"50      + description.getMethodName());51    }52    @Override53    protected void failed(Throwable e, Description description) {54      Logger.getAnonymousLogger().log(Level.WARNING, "failed:" + description.getMethodName(),e);55    }56    @Override57    protected void finished(Description description) {...Source:ZKTestCase.java  
...39    }40    @Rule41    public MethodRule watchman = new TestWatchman() {42        @Override43        public void starting(FrameworkMethod method) {44            // By default, disable starting a JettyAdminServer in tests to avoid45            // accidentally attempting to start multiple admin servers on the46            // same port.47            System.setProperty("zookeeper.admin.enableServer", "false");48            // ZOOKEEPER-2693 disables all 4lw by default.49            // Here we enable the 4lw which ZooKeeper tests depends.50            System.setProperty("zookeeper.4lw.commands.whitelist", "*");51            testName = method.getName();52            LOG.info("STARTING " + testName);53        }54        @Override55        public void finished(FrameworkMethod method) {56            LOG.info("FINISHED " + testName);57        }58        @Override...Source:junit4_rule.java  
...25            String info = description.getClassName() + " " + description.getMethodName();26            System.out.println("failedï¼" + info);27        }28        @Override29        protected void starting(Description description) {30            super.starting(description);31            String info = description.getClassName() + " " + description.getMethodName();32            System.out.println("startingï¼" + info);33        }34    };35    @Test36    public void test1() throws InterruptedException {37        Thread.sleep(3000);38        System.out.println("test1");39    }40    @Test41    public void test2() throws InterruptedException {42        Thread.sleep(3000);43        System.out.println("test2");44    }45    @Test46    public void test3(){...Source:BaseTest.java  
...18    public final ExpectedException exception = ExpectedException.none();19    @Rule20    public TestWatcher testWatcher = new TestWatcher() {21        @Override22        protected void starting(final Description description) {23            String methodName = description.getMethodName();24            String className = description.getClassName();25            className = className.substring(className.lastIndexOf('.') + 1);26            System.out.println("\n>>>>>>>>>>>>>>>> Starting JUnit-test: " + className + "." + methodName);27        }28        @Override29        protected void finished(Description description) {30            String methodName = description.getMethodName();31            String className = description.getClassName();32            className = className.substring(className.lastIndexOf('.') + 1);33            System.out.println("<<<<<<<<<<<< Ended JUnit-test: " + className + "." + methodName + "\n");34        }35    };36}...Source:AbstractTest.java  
...2627	@Rule28	public TestRule watchman = new TestWatcher() {29		@Override30		protected void starting(Description description) {31			String methodName = StringUtil.defaultIfBlank(description.getMethodName(), "before... ");32			String className = StringUtil.substringAfterLast(description.getClassName(), ".");33			log.debug("starting... test className: {}, methodName: {}", className, methodName);34		}35	};3637}
...Source:ModuleTest.java  
...21    public TestName name = new TestName();22    String ClassName;23    @Rule24    public TestRule watcher = new TestWatcher() {25        protected void starting(Description description) {26            ClassName = description.getClassName();27        }28    };2930    @Test31    public void createAccount() {32        loginSteps.givenILaunchURL();33        moduleSteps34                .createAccount(ClassName + ":" + name.getMethodName(), false);35        loginSteps.Logout();36    }3738}
starting
Using AI Code Generation
1    public TestName name = new TestName();2    public void testA() {3        System.out.println(name.getMethodName());4    }5    public void testB() {6        System.out.println(name.getMethodName());7    }starting
Using AI Code Generation
1import org.junit.rules.TestName;2import org.junit.Rule;3public class TestNameRuleExample {4    public TestName name = new TestName();5    public void testA() {6        System.out.println(name.getMethodName());7    }8    public void testB() {9        System.out.println(name.getMethodName());10    }11}12import org.junit.rules.Timeout;13import org.junit.Rule;14public class TimeoutRuleExample {15    public Timeout globalTimeout = Timeout.seconds(10);16    public void testInfiniteLoop1() {17        while (true) {18        }19    }20    public void testInfiniteLoop2() {21        while (true) {22        }23    }24}25import org.junit.rules.Verifier;26import org.junit.Rule;27public class VerifierRuleExample {28    public Verifier verifier = new Verifier() {29        protected void verify() throws Throwable {30            System.out.println("verifying...");31        }starting
Using AI Code Generation
1public void test() throws Exception {2    System.out.println(name.getMethodName());3}4public void test() throws Exception {5    System.out.println(name.getMethodName());6}7public void test() throws Exception {8    Thread.sleep(2000);9}10public void test() throws Exception {11    Thread.sleep(2000);12}13@Test(expected=IndexOutOfBoundsException.class)14public void test() {15    new ArrayList<Object>().get(1);16}17public void test() {18    thrown.expect(IndexOutOfBoundsException.class);19    new ArrayList<Object>().get(1);20}21public void setUp() throws Exception {22    server = new Server(PORT);23    server.start();24}25public void tearDown() throws Exception {26    server.stop();27}28public void setUp() throws Exception {29    server = new Server(PORT);30    server.start();31}32public void tearDown() throws Exception {33    server.stop();34}35public void test() throws Exception {36    File created = folder.newFile("myfile.txt");37    File createdFolder = folder.newFolder("subfolder");38}39public void test() throws Exception {40    File created = folder.newFile("myfile.txt");41    File createdFolder = folder.newFolder("subfolder");42}43public void test() {44}45public Verifier verifier = new Verifier() {46    protected void verify() throws Throwable {47    }48};starting
Using AI Code Generation
1public void testAdd() {2    System.out.println("Test method name: " + testName.getMethodName());3    assertEquals(2, calculator.add(1, 1));4}5public void testSubtract() {6    System.out.println("Test method name: " + testName.getMethodName());7    assertEquals(0, calculator.subtract(1, 1));8}9public void testMultiply() {10    System.out.println("Test method name: " + testName.getMethodName());11    assertEquals(1, calculator.multiply(1, 1));12}13public void testDivide() {14    System.out.println("Test method name: " + testName.getMethodName());15    assertEquals(1, calculator.divide(1, 1));16}17public void testSquare() {18    System.out.println("Test method name: " + testName.getMethodName());19    assertEquals(1, calculator.square(1));20}21public void testSum() {22    System.out.println("Test method name: " + testName.getMethodName());23    assertEquals(6, calculator.sum(new int[] { 1, 2, 3 }));24}25public void testFactorial() {26    System.out.println("Test method name: " + testName.getMethodName());27    assertEquals(1, calculator.factorial(1));28}29public void testIsEven() {30    System.out.println("Test method name: " + testName.getMethodName());31    assertTrue(calculator.isEven(2));32}starting
Using AI Code Generation
1public class TestNameRule extends TestRule {2    private TestName testName = new TestName();3    public Statement apply(Statement base, Description description) {4        return super.apply(base, description);5    }6    public void starting(Description description) {7        String methodName = testName.getMethodName();8        String className = description.getClassName();9        System.out.println("Starting test " + className + "." + methodName);10    }11}LambdaTest also has a detailed JUnit tutorial explaining its features, importance, advanced use cases, best practices, and more to help you get started with running your automation testing scripts.
Here are the detailed JUnit testing chapters to help you get started:
You can also check out our JUnit certification if you wish to take your career in Selenium automation testing with JUnit to the next level.
Get 100 minutes of automation test minutes FREE!!
