[英]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?我有一个返回类型为 object 的方法。如何为此创建测试用例? How do I mention that the result should be an object?我如何提及结果应该是 object?
eg:例如:
public Expression getFilter(String expo)
{
// do something
return object;
}
try Something like this.尝试这样的事情。 If the return-type of your function is Object
then replace Expression
by Object
:如果您的 function 的返回类型是Object
则将Expression
替换为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");
}
}
In your example the returntype is Expression?在您的示例中,返回类型是 Expression? I don't understand the question, could you elaborate?我不明白这个问题,你能详细说明一下吗?
The function is even unable to return anything other than Expression (or a derived type or null). function 甚至无法返回 Expression(或派生类型或 null)以外的任何内容。 So "checking the type" would be pointless.所以“检查类型”将毫无意义。
[TestMethod()]
public void FooTest()
{
MyFoo target = new MyFoo();
Expression actual = target.getFilter();
Assert.IsNotNull(actual); //Checks for null
Assert.IsInstanceOfType(actual, typeof(Expression)); //Ensures type is Expression
}
I am assuming C# here;我在这里假设 C#; you haven't tagged your question nor mentioned the language in your question.你没有标记你的问题,也没有提到你问题中的语言。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.