mercredi 31 mai 2017

Test is not failing even though assertion has failed

I have the same issue to one stated in this question, but I would cover it with a code sample that someone can answer it.

The thing is that I have 2 classes. One of them contains @Test method which calls validation where all asserts are put. The problem arises when that validation method is called via reflection, like this:

public class TestClass {
    @Test
    public void test() {
      ValidationClass validationClass = new ValidationClass();
      validationClass.getClass().getDeclaredMethod("validate").invoke(validationClass, null);
    }
}

public class ValidationClass {
    public void validate() {
        Assert.fail("some message");
    }
}

In this case, the overall test will pass even though assertion failed in validation method. How to fix it? In case I am not using reflection:

@Test
public void test() {
  ValidationClass validationClass = new ValidationClass();
  validationClass.validate();    // NOT USING REFLECTION
}

it will work, but that's not what I want.

Aucun commentaire:

Enregistrer un commentaire