在JMockit中模拟测试类的私有方法

在JMockit中模拟测试类的私有方法

本文介绍了在JMockit中模拟测试类的私有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的测试类(CUT) - 一个ejb - 我有一个私有方法getConnection。
我想测试CUT的另一种方法,但这种方法会在手前失败。

In my class under test (CUT) - an ejb - I have a private method "getConnection".I want to test another method of the CUT but this method will fail before hand.

我尝试过如下所示,但调用是错误的。我不想调用该方法,我想将其存根。但是怎么样? ('connection'是存根)

I tried it like shown below, but "invoke" is wrong. I don't want to invoke the method, I want to stub it. But how? ('connection' is a stub)

new NonStrictExpectations() {
  {
    invoke(archivingBean, "getConnection");result = connection;
  }
};
archivingBean.moveCreditBasic2Archive(new Date());


推荐答案

您的测试是正确的,但它缺少声明对于模拟类型。在这种情况下,EJB类。

Your test is correct, except that it's missing a declaration for the mocked type. The EJB class, in this case.

通常,模拟类型是完全模拟的(所有方法)。在这种情况下,您可以向测试方法声明一个 @Mocked MyEJB archivingBean 参数。例如。

Normally, mocked types are mocked in full (all methods). In such cases, you would declare a @Mocked MyEJB archivingBean parameter to the test method, for example.

For 部分模拟,另一方面,您使用 NonStrictExpectations(Object ...)构造函数,如下所示:

For partial mocking, on the other hand, you use the NonStrictExpectations(Object...) constructor, like this:

new NonStrictExpectations(archivingBean) {{ // <== note the argument here
    invoke(archivingBean, "getConnection"); result = connection;
}};

这篇关于在JMockit中模拟测试类的私有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:57