如何为返回对象的方法编写测试用例

Jes*_*sie 3 java junit object testcase

我有一个方法,其返回类型是对象。我如何为此创建一个测试用例?我如何提及结果应该是一个对象?

例如:

public Expression getFilter(String expo)
{
    // do something
    return object;
}
Run Code Online (Sandbox Code Playgroud)

Sim*_*ant 5

尝试这样的事情。如果函数的返回类型Object替换ExpressionObject

//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");
     }
}
Run Code Online (Sandbox Code Playgroud)