ip696
ip696

Reputation: 7104

How can I create unit test for void method with try...catch inside?

I create simple service for example:

public class MyService {

    public void process() {

        try {
            CustomerMessagesService customerMessagesService = new CustomerMessagesService();
            String message = customerMessagesService.getMessage();

            // another logic which can throw an exception                

            SpamCenterService spamCenterService = new SpamCenterService();
            spamCenterService.sendAdvertise(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

This service calls from Scheduler each 1 second.

customerMessagesService return message with an advertisement text or throw an exception if this message contains illegal text. If customerMessagesService return success text - it to send to spamCenterService and if customerMessagesService throw an exception - it exception just logged. There is another logic which can throw exception between calls of these services. This exception logs too.

Now I want to create a unit test for this method. But I don't understand how to test the void method with try..catch block.

I create this:

public class MyServiceTest {

    private MyService myService;

    @Before
    public void setUp() {
        myService = new MyService();
    }

    @Test
    public void process() {
        myService.process();
    }
} 

But it is always a success because the process method doesn't throw exceptions. How can I test this method?

EDIT

I find one solution but I am not sure about it.

public class MyServiceImpl implements MyService {

    public void process() {
        try {
           doIt();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void doIt() throws Exception {
      CustomerMessagesService customerMessagesService = new CustomerMessagesService();
            String message = customerMessagesService.getMessage();

            // another logic which can throw an exception                

            SpamCenterService spamCenterService = new SpamCenterService();
            spamCenterService.sendAdvertise(message);
    }
}

And test doIt() method in MyServiceImpl.

Upvotes: 0

Views: 3181

Answers (1)

Nicola Ambrosetti
Nicola Ambrosetti

Reputation: 2504

In short do the following:

  • Move object creation out of your method
  • Create mocks (with e.g. Mockito) and inject those
  • verify that the mocks were used as expected

A concrete example below:

public class MyService {

    private CustomerMessagesService customerMessagesService;
    private SpamCenterService spamCenterService;

    //inject dependencies
    public MyService(CustomerMessagesService customerMessagesService, SpamCenterService spamCenterService) {
        this.customerMessagesService = customerMessagesService;
        this.spamCenterService = spamCenterService;
    }

    public void process() {
        try {
            String message = customerMessagesService.getMessage();

            // another logic which can throw an exception

            spamCenterService.sendAdvertise(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


public class MyServiceTest {

    private MyService myService;
    // mock dependencies with Mockito
    private CustomerMessagesService customerMessagesService = Mockito.mock(CustomerMessagesService.class);
    private SpamCenterService spamCenterService = Mockito.mock(SpamCenterService.class);

    @Before
    public void setUp() {
        myService = new MyService(customerMessagesService, spamCenterService);
    }

    @Test
    public void process() {
        myService.process();

        Mockito.verify(customerMessagesService).getMessage();
        ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class);
        Mockito.verify(spamCenterService).sendAdvertise(messageCaptor.capture());
        assertThat(messageCaptor.getValue(), is(nullValue()));
    }

    @Test
    public void processWithSpecificCustomerMessageServiceBehaviour() {
        Mockito.given(customerMessagesService.getMessage()).willReturn("expectedString");

        myService.process();

        ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class);
        Mockito.verify(spamCenterService).sendAdvertise(messageCaptor.capture());
        assertThat(messageCaptor.getValue(), is("expectedString"));
    }

    @Test
    public void processCatchExceptions() {
        Mockito.given(customerMessagesService.getMessage()).willThrow(new Exception("Bad message"));

        myService.process();

        // if exception is thrown then the code should not reach spamCenterService.sendAdvertise()
        Mockito.verify(spamCenterService, Mockito.never()).sendAdvertise(Mockito.anyString());
    }
} 

Note that you can also setup mocks to throw specific exceptions or return specific values so that you can test all possible scenarios.

Upvotes: 2

Related Questions