在单元测试中检查工厂的结果 [英] Checking the results of a Factory in a unit test

查看:87
本文介绍了在单元测试中检查工厂的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一些具有类似行为的类,它们都实现了相同的接口。我实现了一个工厂,它创建适当的对象并返回接口。我正在为工厂写一个单元测试。你得到的只是对象的接口。
测试工厂工作正常的最佳方法是什么?

I have developed some classes with similar behavior, they all implement the same interface. I implemented a factory that creates the appropriate object and returns the interface. I am writing a unit test for the factory. All you get back is an interface to the object. What is the best way to test that the factory has worked correctly?

我想知道Java的答案,但是如果有解决方案的话穿过我想知道的语言。

I would like to know the answer in Java, but if there is a solution that crosses languages I would like to know it.

答案中的第2号,会像其他答案一样完成吗?如果是这样,我将标记另一个接受的答案并重新提出我的问题,以解决返回界面的工厂以及您不知道哪种类型的具体类实现了界面,以及您知道具体类是什么的情况使用。

Number 2. in the answer, would be done like the other answer? If so I will mark the other answer accepted as well and reword my question to adress both a factory where an interface is returned and you have no clue what type of concrete class implemented the interface, and the case where you do know what concrete class was used.

推荐答案

由于我不知道您的工厂方法如何,我现在可以建议的是

Since I don't know how your factory method looks like, all I can advise right now is to


  1. 检查对象是否是您正在寻找的正确具体实现:

  1. Check to see the object is the correct concrete implementation you were looking for:

IMyInterface fromFactory = factory.create(...);  
Assert.assertTrue(fromFactory instanceof MyInterfaceImpl1);


  • 您可以检查工厂是否使用有效的实例变量设置具体实例。

  • You can check if the factory setup the concrete instances with valid instance variables.

    这篇关于在单元测试中检查工厂的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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