0

I want to create a testcase to test it the authorization is valid or not when I call the service.

I Mock my service that will create a new Person. The service will do some logic and validation before persisting the Person in the database. One of the validation, is to validate if the user is authorized to do that. If it's not authorization, there will be a exception that will be thrown.

That validation is done in my service.

The problem, is that I don't know how to create the test case to reproduce that usecase. I don't know how to mock an exception thrown by a mocked object.

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

Here an example how functionality look like :

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);
        }
    }

}
Sebastien Dionne
  • 757
  • 2
  • 19
  • 34

2 Answers2

1

Here you are :

//  will call SecurityUtil.isValid(authorization);
messageService.createPerson(person);
result = new NOTAuthorizedException()
bobuns
  • 277
  • 1
  • 4
  • 12
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");

 }
}

Alternate solution: You could use MockUp. This feature allows you to give an alternate definition to your mock methods.

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

  }
}
Touchstone
  • 5,575
  • 7
  • 41
  • 48
  • I have a little problem with that solution. securityUtil.isValid will send a InvalidAuthorizationException and it's what I want to test. Maybe a simpler solution will be to refactor to be able to inject securityUtil. – Sebastien Dionne Mar 30 '15 at 16:40
  • @SebastienDionne do you want InvalidAuthorizationException to be thrown by the createPerson method? – Touchstone Mar 30 '15 at 16:47