12

I'm trying to mock this method:

boolean login() throws SftpModuleException;

Mocking code is:

Mockito
    .when(this.sftpService.login())
    .thenReturn(true);

Since, login() throws an SftpModuleException, compiler tells me that this exception has to be handled.

Is there any work around due to this exception will never be thrown?

Jordi
  • 20,868
  • 39
  • 149
  • 333
  • 1
    Possible duplicate of [Using mockito to test methods which throw uncaught custom exceptions](https://stackoverflow.com/questions/9238702/using-mockito-to-test-methods-which-throw-uncaught-custom-exceptions) – Ori Marko Feb 04 '19 at 10:20

2 Answers2

8

Consider having your @Test method simply declare the exceptions being thrown, or even declaring throws Exception.

@Test
public void testFoo() throws Exception {
  // mocking and test code here
}
orip
  • 73,323
  • 21
  • 116
  • 148
6

I think you can add this to a method signature

@Test
public void test() throws SftpModuleException {

  Mockito
    .when(this.sftpService.login())
    .thenReturn(true);
  // code
}
dehasi
  • 2,644
  • 1
  • 19
  • 31