如何为方法返回对象编写测试用例 [英] How to write a test case for a method returning object

查看:105
本文介绍了如何为方法返回对象编写测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回类型为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?

例如:

public Expression getFilter(String expo)
{
    // do something
    return object;
}

推荐答案

尝试类似的方法.如果函数的返回类型为Object,则将Expression替换为Object:

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
        //checks if the returned object is of class Expression
        assertTrue( true, myReturnedObject instaceof Expression);
    }catch(Exception e){
        // let the test fail, if your function throws an Exception.
        fail("got Exception, i want an Expression");
     }
}

这篇关于如何为方法返回对象编写测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆