Silver137
Silver137

Reputation: 151

Junit tests, verify that the value returned is a int basic type

I need to do a JUnit test and verify that the value returned by a function is an int value.

I have to try that

assertTrue(shelving.numberOfBottles() instanceof java.lang.Integer);

But it results in an "Incompatible conditional operand types int and Integer".

Upvotes: 0

Views: 1791

Answers (1)

swayamraina
swayamraina

Reputation: 3158

I can think of two ways to check this,

import org.junit.Assert;
import org.junit.Test;

public class JunitReturnTypeTest {

    // example method
    private static Object returnInt() {
        int a=5;
        return a;
    }

    @Test
    public void testInstance() {
        Assert.assertTrue(Integer.class.isInstance(returnInt()));
    }

    @Test()
    public void testCasting() {
        try {
            int a = (Integer) returnInt();
        } catch (ClassCastException e) {
            Assert.fail();
        }
    }

}

Upvotes: 1

Related Questions