【问题标题】:How mock method in a Mocked object with JMockit?如何使用 JMockit 在 Mocked 对象中模拟方法?
【发布时间】:2015-03-18 18:17:47
【问题描述】:

我想创建一个测试用例来测试我调用服务时授权是否有效。

我模拟我的服务,它将创建一个新的人。在将 Person 持久保存在数据库中之前,该服务将执行一些逻辑和验证。验证之一是验证用户是否被授权这样做。如果不是授权,就会抛出异常。

验证在我的服务中完成。

问题是我不知道如何创建测试用例来重现该用例。我不知道如何模拟被模拟对象抛出的异常。

@RunWith(JMockit.class)
public class RESTServiceTest {
    @Mocked
    private IMessageService messageService;

    private final IRESTService service = new RESTService();

    @Test
    public void testNew() throws Exception {
        final Person person = new Person();

        new NonStrictExpectations() {
        {
            Deencapsulation.setField(service, messageUtil);
            Deencapsulation.setField(service, messageService);

            //  will call securityUtil.isValid(authorization); //that will throw a InvalidAuthorizationException
            messageService.createPerson(person, authorization);
            //messageService will catch the InvalidAuthorizationException and throw an exception : NOTAuthorizedException();
        }
    };

    Person createdPerson = service.newPerson(person, "INVALID AUTHORIZATION");

这里是功能的示例:

public class RESTService implements IRESTService {

    public Person newPerson(Person person, String authorization){
        ...
        messageService.createPerson(person, authorization);

        ...

        return person;

    }

}

public class MessageService implements IMessageService {

    public void createPerson(Person person, String authorization){

        try {
            ... // private methods
            securityUtil.isValid(authorization); // will throw InvalidAuthorizationException is invalid
            ...
            create(person);
            ...
        } catch(InvalidAuthorizationException e){
            log.error(e);
            throw new NOTAuthorizedException(e);
        }
    }

}

【问题讨论】:

    标签: junit mocking jmockit


    【解决方案1】:

    你来了:

    //  will call SecurityUtil.isValid(authorization);
    messageService.createPerson(person);
    result = new NOTAuthorizedException()
    

    【讨论】:

      【解决方案2】:
      @RunWith(JMockit.class)
      public class RESTServiceTest {
      @Mocked
      private IMessageService messageService;
      
      private final IRESTService service = new RESTService();
      
        @Test(expected=NOTAuthorizedException.class)
        public void testNew() throws Exception {
          final Person person = new Person();
      
          new NonStrictExpectations() {
          {
              Deencapsulation.setField(service, messageUtil);
              Deencapsulation.setField(service, messageService);
      
              //  will call securityUtil.isValid(authorization); //that will throw a InvalidAuthorizationException
              messageService.createPerson(person, authorization);
              result=new NOTAuthorizedException();
          }
        };
      
        Person createdPerson = service.newPerson(person, "INVALID AUTHORIZATION");
      
       }
      }
      

      替代解决方案: 你可以使用 MockUp。此功能允许您为模拟方法提供替代定义。

      @RunWith(JMockit.class)
      public class RESTServiceTest {
      @Mocked
      private IMessageService messageService;
      
      private final IRESTService service = new RESTService();
      
        @Test(expected=NOTAuthorizedException.class)
        public void testNew() throws Exception {
          final Person person = new Person();
      
           new MockUp<MessageService>() {
      
            @Mock 
            public void createPerson(Person person, String authorization){
      
              ... 
              // Use Deencapsulation class's invoke() method to call private methods
              securityUtil.isValid(authorization); // will throw  InvalidAuthorizationException is invalid
              ...
              ...
            }
           });
      
          Person createdPerson = service.newPerson(person, "INVALID AUTHORIZATION");
      
        }
      }
      

      【讨论】:

      • 我对那个解决方案有一点问题。 securityUtil.isValid 将发送一个 InvalidAuthorizationException ,这是我想要测试的。也许更简单的解决方案是重构能够注入 securityUtil。
      • @SebastienDionne 你想让 createPerson 方法抛出 InvalidAuthorizationException 吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-24
      • 2014-12-12
      相关资源
      最近更新 更多