如何使用EasyMock测试内部类 [英] How to test the inner classes by using EasyMock

查看:356
本文介绍了如何使用EasyMock测试内部类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是EasyMock的新手。我需要使用EasyMock测试类,但是这里的问题是我的类具有内部类,并且该内部类在外部类的方法中无效,并通过传递一些参数来调用内部类的方法。我不确定如何测试此类。

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 test this class.

下面是一些示例代码。

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..
        }
    }


推荐答案

在Easymock中,有些东西不能作为静态方法调用和构造函数调用来模拟。您可能会更改代码,以便能够使用Easymock对其进行测试,因为在sendEmailNotice方法中,有一个您可能要模拟的调用,但您不能。可以对MailSender.send()调用进行模拟。我们可以这样做,创建一个包含可以嘲笑对MailSender的调用的类。

There are some things that you cannot mock with Easymock as calls to static methods and calls to constructors. You might change your code to be able to test it with Easymock because in the method sendEmailNotice there is a call that you may like to mock but you can't. A mock for the MailSender.send() call would be appropriate. We could do that creating a class that contains the call to the MailSender that could be mocked.

public class MailWrapper {

  public MailWrapper () {
  }

  public void send ( MimeMessagePrepator eNotice) {
    MailSender.send(eNotice);
  }

}

您可以使用此实例

public class ServiceClass implements ServiceInterface {

   //Added as a member  
   private MailWrapper mw;

   public ServiceClass () {
     this.mw = new MailWrapper();
   }

   //Constructor added for test purposes  
   public ServiceClass (MailWrapper mw) {
      this.mw = mw;
   }

   public void updateUSer(USer) {
      //some logic over here.
      sendEmailNotice(subject, vTemplate);
   }

   private sendEmailNotice(subject, vTemplate) {

       MimeMessagePrepator eNotice = new PrepareEmailNotice(subject, vTemplate);
       mw.send( prepator );   
   }


...

}

ServiceClass类的测试如下:

The test of the ServiceClass class would be like this:

public class ServiceClassTest {

    @Test
    public void testUpdateUser() {

        String subject = "Expected subject";
        String vTemplate = "Expected vTemplate";

        MimeMessagePrepator eNotice = new PrepareEmailNotice(subject,vTemplate);       

        MailWrapper mwMock = createMock (MailWrapper.class);

        //expecting the void call to the MailWrapper
        mwMock.send(eNotice);
        //other expectations...

        replay(mwMock);
        ServiceClass sc = new ServiceClass(mwMock);
        sc.updateUser(new User());
        verify(mwMock);
        //some asserts
    }
}

消息中您在问内部类,但是我认为内部类的测试包含在外部类的测试中,您将
不需要分开测试。如果PrepareEmailNotice具有复杂的代码并应进行模拟,则可以进行更改,添加一个MimeMessagePrepator成员,可以将
作为参数传递给构造函数(如MailWrapper)。但是我认为,如果它具有复杂且必须模拟的代码,也许它就不是内部类。

In the message you were asking about the inner class, but I think that the test of the inner class is contained in the test of the outer class, and you would not need to test it apart. In case PrepareEmailNotice has complex code and should be mocked, you could do changes, adding a MimeMessagePrepator member that could be passed as a parameter in the constructor like the MailWrapper. But I think that in case it has complex and have-to-be-mocked code, maybe it would not be an inner class.

此外,您可以使用 Powermock ,可让您模拟静态调用和构造函数调用,以防万一您不想更改您的测试框架。

Also, you could use Powermock, that allows you to mock static calls and constructor calls, in case you don't mind to change your test framework.

希望它会有所帮助。
的问候。

Hope it helps. Regards.

这篇关于如何使用EasyMock测试内部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆