junit-team / junit4

A programmer-oriented testing framework for Java.
https://junit.org/junit4
Eclipse Public License 1.0
8.51k stars 3.24k forks source link

Improve assumeNotNull to allow custom message #1723

Closed kaanmertcakmak closed 2 years ago

kaanmertcakmak commented 2 years ago

Added assumeNotNull method that allows custom message, just like other assertions and assumptions

After added

        @Test
        public void testAssumeNotNull() {
            Object[] objects = {1, 2, null};
            assumeNotNull("One of the objects is null",objects);
        }

Output

org.junit.AssumptionViolatedException: One of the objects is null: got: <[1, 2, null]>, expected: every item is not null

In my company, we use junit4 and I tried to add custom message into the assumeNotNull method, just like other assertions and assumption methods of junit4. Then I realized that, assumeNotNull only accepts objects, so I thought that this could be useful

kcooney commented 2 years ago

This change would alter the behavior of some tests. The following test would pass in 4.13 but fail with your changes:

@Test
public void testPull1723() {
    String object1 = null;
    String object2 = "2";
    String object3 = "3";

    try {
        assumeNotNull(object1, object2, object3);
    } catch (AssumptionViolatedException expected) {
        return;
    }
    fail();
}

Why do you want custom messages for assumeNoNull()? The message of a AssumptionViolatedException doesn't matter; the exception simply indicates that the test should reported as skipped.

kaanmertcakmak commented 2 years ago

This change would alter the behavior of some tests. The following test would pass in 4.13 but fail with your changes:

@Test
public void testPull1723() {
    String object1 = null;
    String object2 = "2";
    String object3 = "3";

    try {
        assumeNotNull(object1, object2, object3);
    } catch (AssumptionViolatedException expected) {
        return;
    }
    fail();
}

Why do you want custom messages for assumeNoNull()? The message of a AssumptionViolatedException doesn't matter; the exception simply indicates that the test should reported as skipped.

Hi, Thanks for answer. I ran the same test and see it is failed. So in my company, we are skipping some scenarios by checking server's version on that environment and if version is higher than some number, then it should be skipped. And also there are some other scenarios that we skip tests conditionally. So displaying the reason of skip in such scenarios, would be useful for our test reports. But I don't think that there is a way to do this in such cases. So we can close this pr