我有一种返回类型是对象的方法。我如何为此创建测试用例?我如何提到结果应该是一个对象?如何为返回对象的方法编写测试用例
例如为:
public Expression getFilter(String expo)
{
// do something
return object;
}
我有一种返回类型是对象的方法。我如何为此创建测试用例?我如何提到结果应该是一个对象?如何为返回对象的方法编写测试用例
例如为:
public Expression getFilter(String expo)
{
// do something
return object;
}
尝试类似这样的东西。如果返回类型的功能是Object
然后通过Object
更换Expression
:
//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");
}
}
在您的例子中,返回类型是表达?我不明白这个问题,你能否详细说明一下?
函数是甚至无法返回除表达式(或派生类型或null)以外的任何东西。所以“检查类型”将毫无意义。
[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
}
我在这里假设C#;你没有标记你的问题,也没有提到你的问题中的语言。
您好我需要junit测试用例。我提到表达式其实际的对象。 – Jessie 2012-04-06 00:28:07
因此,下次为'java'和'junit'标记问题,并确保示例代码准确地重现或演示了您的问题;-)(这次为您做了)。我认为关键是[instanceof](http://www.java2s.com/Tutorial/Java/0060__Operators/TheinstanceofKeyword.htm),但我不是Java大师:-)你可能也想看看http:/ /stackoverflow.com/questions/496928/what-is-the-difference-between-instanceof-and-class-isassignablefrom – RobIII 2012-04-06 00:32:54
这是不明确。你的方法返回一个表达式。 Java中的所有东西都是“对象”,包括你的“表达式”。你想要测试什么? – Guillaume 2012-04-06 22:55:37