简体   繁体   English

junit 用于测试返回的测试用例 object

[英]junit testcase for test returning object

I have a method in which the return type is Object. How can I create testcase for this?我有一个返回类型为 Object 的方法。如何为此创建测试用例? How to mention the result should be a object?怎么提结果应该是object?

My exact method is here.我的确切方法在这里。

public Object getData(String type, String expression)
{
    // do something here
    return Object;
}

How can I write a testcase for this method?我怎样才能为这个方法写一个测试用例?

It would be better to rename your method createXXX and give it a more interesting return type.最好重命名您的方法 createXXX 并为其提供更有趣的返回类型。 Then you can build some test to build a specific object of a specific type and test if it corresponds to what you expect.然后你可以构建一些测试来构建特定类型的特定 object 并测试它是否符合你的期望。

All those factory methods can share a common kernel that you can test seperatly.所有这些工厂方法都可以共享一个公共的 kernel,您可以单独对其进行测试。

Have a specific method for each type built means having a specific test for each method, it will be more clear for everyone and easier to test.为每个类型构建一个特定的方法意味着对每个方法都有一个特定的测试,这样大家会更清楚,也更容易测试。

If all your return types share a common behavior that you can use for testing, then put this in an interface and set the return type to be an interface.如果您所有的返回类型共享一个可用于测试的通用行为,那么将其放入一个接口并将返回类型设置为一个接口。

To soum up, having a method whose return type is object obliges every caller to subclass the result, knowing more or less the inner worjk of your method and this is a bad idea: clients will have to change when service interns change.总而言之,拥有一个返回类型为 object 的方法会迫使每个调用者对结果进行子类化,或多或少地了解你的方法的内部工作,这是一个坏主意:当服务实习生改变时,客户将不得不改变。 You would be better to provide something more stable like returning an object with a given interface to interact with.你最好提供一些更稳定的东西,比如返回一个 object 与给定的交互界面。

You could test that the Object is not null.您可以测试 Object 不是 null。

The test that the Object is of the correct type you are looking for.测试 Object 是您要查找的正确类型。

Or...要么...

Have it return a concrete class.让它返回具体的 class。

Paul保罗

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM