如何使用jmockit模拟InitialContext类?
我在测试中使用 jmockit,并且对于我希望测试的一个类,直接使用 InitialContext
。 所以我有以下内容:
public class MyClass {
public void myMethod() {
InitialContext ic = new InitialContext();
javax.mail.Session mailSession = ic.lookup("my.mail.session");
// rest of method follows.....
}
在我的测试用例中,我调用它来使用我的“模拟”InitialContext
类:
Mockit.redefineMethods(InitialContext.class, MockInitialContext.class);
使用 jmockit 模拟 InitialContext
类的最佳方法是什么。
我已经尝试了几种方法(例如使用我自己的 MockInitialContextFactory
),但不断陷入相同的错误:
NoClassDefFoundError: my.class.MockInitialContext
从我在 Google 上看到的情况来看,使用 JNDI 进行模拟非常令人讨厌。 请问任何人都可以为我提供一些指导,或者指出解决方案吗? 我们将非常感激。 谢谢。
I'm using jmockit with my tests and with one class I wish to test, uses InitialContext
directly. So I have the following:
public class MyClass {
public void myMethod() {
InitialContext ic = new InitialContext();
javax.mail.Session mailSession = ic.lookup("my.mail.session");
// rest of method follows.....
}
In my test case, I call this to use my "mocked" InitialContext
class:
Mockit.redefineMethods(InitialContext.class, MockInitialContext.class);
What is the best way to mock the InitialContext
class with jmockit.
I've already tried a few ways (such as using my own MockInitialContextFactory
), but keeping stumbling into the same error:
NoClassDefFoundError: my.class.MockInitialContext
From what I can see on Google, mocking with JNDI is quite nasty. Please can anyone provide me with some guidance, or point me to a solution? That would be much appreciated. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
自从有人在这里发帖以来已经一年了,但自从最近我使用 JMockit 模拟 EJB 调用以来,我觉得分享是正确的事情。 (即使我没有测试它,代码应该非常相似)
您可以将一些模拟对象定义为您的测试用例中的字段,例如:
然后在您的 testXXX 方法中您可以定义您的 Expectations(),这样做之后只需调用您想要测试的方法。
I now its being a year since someone posted here, but since recently i was mocking EJB calls using JMockit i felt is the right thing to share. (Even though i haven't test it the code should be very similar)
You can define some Mocked object as field in your TestCase like:
then in your testXXX method you can define your Expectations(), after doing so is just matter of calling the method you want o test.
一般来说,要模拟 JNDI,您需要使用一个框架,例如 EJBMock,它可以提供一个模拟容器来部署您的 bean。
另一种选择是重构从代码中创建上下文,以便将其传入(这是依赖注入重构),然后您应该能够随意替换模拟。
In general, to mock JNDI, you will need to use a framework, such as EJBMock, that can provide a mock container in which to deploy your beans.
The other alternative is to refactor creating the context out of your code so that it is passed in (this is dependency injection refactoring), and then you should be able to substitute a mock at will.
您收到 NoClassDefFoundError 因为 my.class.MockInitialContext 不存在。 如果要将其作为参数传递给 Mockit.redefineMethods(),则需要创建该类。 您的 MockInitialContext 类只需要一个名为 Lookup() 的方法,该方法接受 String 参数并返回 javax.mail.Session。
我喜欢 JMockit 注释,但您可以浏览该页面的其余部分以获取其他示例如何使用JMockit。
You're getting the NoClassDefFoundError because my.class.MockInitialContext doesn't exist. You need to create that class if you're going to pass it as an argument to Mockit.redefineMethods(). Your MockInitialContext class would just need a method named lookup() that accepts a String parameter and returns a javax.mail.Session.
I like JMockit annotations, but you can look through the rest of that page for other examples of how to use JMockit.