javaunit-testingjunitjunit4junit3

Junit assert OR condition in my test case


In my test case, I get an integer value:

int val = getXXX();

Then, I would like to check if val either equals to 3 or equals to 5 which is OK in either case. So, I did:

assertTrue(val == 3 || val==5);

I run my test, the log shows val is 5, but my above assertion code failed with AssertionFailedError. Seems I can not use assertTrue(...) in this way, then, how to check true for OR condition?


Solution

  • ive tried to write quick test:

    @Test
    public void testName() {
        int i = 5;
        junit.framework.Assert.assertTrue(i == 3 || i == 5);
    
    }
    

    its passing always so i guess there is some inbetween code when your value is changed. You can use

    org.junit.Assert.assertEquals(5, i);
    

    to check value - this assertion will print out nice info whats wrong, for example:

    java.lang.AssertionError: 
    Expected :4
    Actual   :5