无法使用 EasyMock 为内部类编写测试用例
我是 EasyMock 的新手。我需要使用 EasyMock 测试我的课程。但这里的问题是我的类有内部类,这个内部类是在外部类的方法中实例化的,并通过传递一些参数来调用内部类的方法。我不确定如何为这种情况编写测试用例。
请帮我为此编写测试用例。
非常感谢任何帮助或建议。
public class ServiceClass implements ServiceInterface {
public void updateUSer(USer) {
//some logic over here.
sendEmailNotice(subject, vTemplate);
}
private sendEmailNotice(subject, vTemplate) {
MimeMessagePrepator eNotice = new PrepareEmailNotice(subject, vTemplate);
MailSender.send( eNotice );
}
public class PrepareEmailNotice implements MimeMessagePrepator {
// some local variables.
public PrepareEmailNotice(subject, vTemplate) {
subject = subject;
vTemplate = vTemplate;
}
public void prepare( MimeMessage message) {
MimeMessageHealper helper = new MimeMessageHealper(message, true);
// setting the mail properties like subject, to address, etc..
}
}
谢谢。
I am new to the EasyMock. I need to test my class using the EasyMock. but here the problem is my class has inner class and this inner class is instatiated in the outer class's method and calling the method of inner class by passing some parameters. I am not sure how to write the test case for this scenario.
Please help me write the test case for this.
Any help or suggetions are highly appreciated.
public class ServiceClass implements ServiceInterface {
public void updateUSer(USer) {
//some logic over here.
sendEmailNotice(subject, vTemplate);
}
private sendEmailNotice(subject, vTemplate) {
MimeMessagePrepator eNotice = new PrepareEmailNotice(subject, vTemplate);
MailSender.send( eNotice );
}
public class PrepareEmailNotice implements MimeMessagePrepator {
// some local variables.
public PrepareEmailNotice(subject, vTemplate) {
subject = subject;
vTemplate = vTemplate;
}
public void prepare( MimeMessage message) {
MimeMessageHealper helper = new MimeMessageHealper(message, true);
// setting the mail properties like subject, to address, etc..
}
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先你需要思考什么是班级责任。
它应该与谁说话?
一旦您清楚地识别了依赖关系,您就需要了解如何在代码中处理它们。
您可能需要执行一些重构才能符合依赖倒置原则。
例如,这里您有对 MailSender 类的依赖项,但您无法模拟它,因为此依赖项是“硬编码”的。
有一个很好的视频:http://www.youtube.com/watch?v=XcT4yYu_TTs
First of all you need to think about what is the class responsibility.
At should it be doing with who should it be speaking?
Once you've clearly identified the dependencies you need to see how you can handle them in your code.
You might need do to perform some refactoring in order to conform to the dependency inversion principle.
For example here you have a dependency to the MailSender class but you won't be able to mock it as this dependency is "hard coded".
There is a good video about that: http://www.youtube.com/watch?v=XcT4yYu_TTs