在JMockit中模拟测试类的私有方法
在我的测试类(CUT) – 一个ejb – 我有一个私有方法“getConnection”。 我想测试CUT的另一种方法,但这种方法会先失败。
我尝试过如下所示,但“调用”是错误的。 我不想调用该方法,我想将其存根。 但是怎么样? (’连接’是存根)
new NonStrictExpectations() { { invoke(archivingBean, "getConnection");result = connection; } }; archivingBean.moveCreditBasic2Archive(new Date());
您的测试是正确的,除了它缺少模拟类型的声明。 在这种情况下,EJB类。
通常,模拟类型会被完全模拟(所有方法)。 在这种情况下,您可以向测试方法声明@Mocked MyEJB archivingBean
参数。
另一方面,对于部分 NonStrictExpectations(Object...)
,使用NonStrictExpectations(Object...)
构造函数,如下所示:
new NonStrictExpectations(archivingBean) {{ // <== note the argument here invoke(archivingBean, "getConnection"); result = connection; }};