无法使用 EasyMock 为内部类编写测试用例

发布于 2024-12-06 05:59:32 字数 985 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方。的韩爷 2024-12-13 05:59:32

首先你需要思考什么是班级责任。
它应该与谁说话?

一旦您清楚地识别了依赖关系,您就需要了解如何在代码中处理它们。
您可能需要执行一些重构才能符合依赖倒置原则。
例如,这里您有对 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文