Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a test case for a method returning object

I have a method for which the return type is object. How do I create a test case for this? How do I mention that the result should be an object?

e.g.:

public Expression getFilter(String expo)
{
    // do something
    return object;
}
like image 288
Jessie Avatar asked Nov 24 '25 03:11

Jessie


1 Answers

try Something like this. If the return-type of your function is Object then replace Expression by Object:

//if you are using JUnit4 add in the @Test annotation, JUnit3 works without it.
//@Test
public void testGetFilter(){
    try {
        Expression myReturnedObject = getFilter("testString");
        assertNotNull(myReturnedObject); //check if the object is != null
        //check if the returned object is of class Expression.
        assertTrue(true, myReturnedObject instanceof Expression);
    } catch(Exception e){
        // let the test fail, if your function throws an Exception.
        fail("got Exception, i want an Expression");
     }
}
like image 96
Simulant Avatar answered Nov 26 '25 15:11

Simulant