2

I have test:

public class MyTest {

@Test
public void test_1(){
    assertTrue(false);
}

@Test
public void test_2(){
    assertTrue(false);
}

and CustomListener:

public class CustomerListener extends RunListener {

   @Override
   public void testFailure(Failure failure) throws Exception {
      if(some_condition) {
        super.testAssumptionFailure(failure);
      } else {
        super.testFailure(failure);
      }
 }

Runing test using maven:

mvn test -Dtest=MyTest.java

CustomerListener worked but all the times tests marked as failed ('some_condition' are true). How I can mark some tests as skipped using CustomerListener?

Marina
  • 51
  • 2
  • Why not directly using the Assume in your unit tests? https://github.com/junit-team/junit4/wiki/assumptions-with-assume – khmarbaise Feb 24 '18 at 20:58

1 Answers1

0

Thank you werner, but I found next solution

 public class CusomIgnoreRule implements TestWatcher {
   @Override
public Statement apply( final Statement base, final Description description ) {
    return new Statement() {
        @Override
        public void evaluate() throws Throwable {
            try {
                base.evaluate();
            }
            catch ( Throwable t ) {
                if(some_condition) {
                   throw new AssumptionViolatedException("do this so our test gets marked as ignored")
                }
                throw t; 
            }
        }
    };
}

and in test:

public class MyTest {

@Rule
public CusomIgnoreRule rule = new CusomIgnoreRule();

@Test
public void test_1(){
    assertTrue(false);
}

@Test
public void test_2(){
    assertTrue(false);
}
Marina
  • 51
  • 2