如何使用JMockit在Mocked对象中使用mock方法?

时间:2015-03-18 18:17:48

标签: junit mocking jmockit

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

我模拟我的服务,将创建一个新人。在将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);
        }
    }

}

2 个答案:

答案 0 :(得分:1)

你在这里:

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

答案 1 :(得分:0)

@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");

  }
}