Run a method if the test fails on Junit


Keva 161

I've added a method to my Selenium framework that takes a screenshot at the end of a test, but it doesn't run all the way to the end because some tests fail. I want to implement a rule to run this screenshot method if the test fails.

It's just like:

@Rule
public Failure fail = new Failure();

if (fail)
{
    // Method
}
Pashupati Rajamanikam

You can use TestWatcherthis hook with failure and success results. You can add your hook logic

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

public class Test1 {

    @Rule
    public TestWatcher watchman = new TestWatcher() {
        @Override
        protected void failed(Throwable e, Description description) {
            System.out.println("failed");
        }
    };

    @Test
    public void test() {
        Assert.assertEquals(2, 4);
    }

}

Related


JUnit's LinkedList method test fails, why?

UserFuser I have this method which will search a LinkedList (named ListNode) and check for chars, and check if they contain uppercase chars, then store it in a new linked list, and return it. I wrote code for this and tested it with JUnit and it failed JUNit (

JUnit's LinkedList method test fails, why?

UserFuser I have this method which will search a LinkedList (named ListNode) and check for chars, and check if they contain uppercase chars, then store it in a new linked list, and return it. I wrote code for this and tested it with JUnit and it failed JUNit (

How to run a single JUnit test method in Eclipse?

Australian Moss: How to selectively run tests in a JUnit test case with multiple @Test annotations? For example, from the code below, how can I run only one test method? @Test public void testHelloEmpty() { assertEquals(h.getName(),""); a

How to run a single JUnit test method in Eclipse?

Australian Moss: How to selectively run tests in a JUnit test case with multiple @Test annotations? For example, from the code below, how can I run only one test method? @Test public void testHelloEmpty() { assertEquals(h.getName(),""); a

How to run a single JUnit test method in Eclipse?

AussieMoss In a JUnit test case with multiple @Test annotations, how does one selectively run tests ? For e.g., from the following code, how does one run just one test method ? @Test public void testHelloEmpty() { assertEquals(h.getName(),"");

How to run a single JUnit test method in Eclipse?

Australian Moss How to selectively run tests in a JUnit test case with multiple @Test annotations? For example, from the code below, how can I run only one test method? @Test public void testHelloEmpty() { assertEquals(h.getName(),""); as

How to run a single JUnit test method in Eclipse?

Australian Moss: How to selectively run tests in a JUnit test case with multiple @Test annotations? For example, from the code below, how can I run only one test method? @Test public void testHelloEmpty() { assertEquals(h.getName(),""); a

Django test case fails to run method

Broowski I'm just starting out with Django, so this might be a little silly, but I don't even know how to google at this point. I have a method that looks like this: def get_user(self,user): return Utilities.get_userprofile(user) The method looks like thi

Django test case fails to run method

Broowski I'm just starting out with Django, so this might be a little silly, but I don't even know how to google at this point. I have a method that looks like this: def get_user(self,user): return Utilities.get_userprofile(user) The method looks like thi

JUnit test fails when annotating method arg @Nonnull

Freddy Boucher: Can someone explain why my JUnit tests are failing: notNullMethodTest() using @NotNull -> success nonnullMethodTest() with @Nonnull - > fails Unexpected exception, expected <java.lang.NullPointerException>, but got <java.lang.IllegalArgumentExc

JUnit test fails with NPE when testing validator method

Sudir I am going through the following tutorial: http://docs.spring.io/docs/Spring-MVC-step-by-step/part4.html I am trying to test the validate method in the class below: public class PriceIncreaseValidator implements Validator { public void validate(Objec

JUnit test fails with NPE when testing validator method

Sudir I am working through the following tutorial: http://docs.spring.io/docs/Spring-MVC-step-by-step/part4.html I am trying to test the validate method in the class below: public class PriceIncreaseValidator implements Validator { public void validate(Obj

JUnit test fails when annotating method arg @Nonnull

Freddy Boucher: Can someone explain why my JUnit tests are failing: notNullMethodTest() using @NotNull -> success nonnullMethodTest() with @Nonnull - > fails Unexpected exception, expected <java.lang.NullPointerException>, but got <java.lang.IllegalArgumentExc

JUnit test fails with NPE when testing validator method

Sudir I am working through the following tutorial: http://docs.spring.io/docs/Spring-MVC-step-by-step/part4.html I am trying to test the validate method in the class below: public class PriceIncreaseValidator implements Validator { public void validate(Obj

junit test fails with exception

Sebastian Dietrich I am using junit to test my Metrics class. When the test runs, I don't get any exceptions, but the test doesn't succeed. I've tried to find all the different questions but haven't found any. Here is its code. public class MetricsTest { pri

jUnit test on JAVA fails

k2wln I'm new here and sorry for the stupid question. I have some problems with my JUnit test: for some reason it shows me an error in index.html. I attach the link to my repository through tests and screens. Thanks! mistake: org.opentest4j.AssertionFailedErro

How to write junit test case for thread run method

Karthick Murugamoorthi I have public void run() method. Inside the run method, I will call the following three methods (CurrentDate, generateReport and update). My doubt is that for run method I don't have any parameter to pass Details object. How to pass Deta

Eclipse: How to run a single inherited JUnit test method

Jens Piegsa I know that Eclipse provides methods to run individual JUnit 4 test methods separately (see Running a single JUnit test in Eclipse ). Now, I'm wondering if there is a way to have this method run when a given concrete class does not include it, but

Eclipse: How to run a single inherited JUnit test method

Jens Piegsa I know that Eclipse provides methods to run individual JUnit 4 test methods separately (see Running a single JUnit test in Eclipse ). Now, I'm wondering if there is a way to have this method run when a given concrete class does not include it, but

Eclipse: How to run a single inherited JUnit test method

Jens Piegsa I know that Eclipse provides methods to run individual JUnit 4 test methods separately (see Running a single JUnit test in Eclipse ). Now, I'm wondering if there is a way to have this method run when a given concrete class does not include it, but

How to write junit test case for thread run method

Karthick Murugamoorthi I have public void run() method. Inside the run method, I will call the following three methods (CurrentDate, generateReport and update). My doubt is that for run method I don't have any parameter to pass Details object. How to pass Deta

How to write junit test case for thread run method

Karthick Murugamoorthi I have public void run() method. Inside the run method, I will call the following three methods (CurrentDate, generateReport and update). My doubt is that for run method I don't have any parameter to pass Details object. How to pass Deta

How to write junit test case for thread run method

Karthick Murugamoorthi I have public void run() method. Inside the run method, I will call the following three methods (CurrentDate, generateReport and update). My doubt is that for run method I don't have any parameter to pass Details object. How to pass Deta