Junit Force to throw Exception on method call



I am trying to throw the exception whenever simpleJdbcCall.execute(namedParameters) called but I see it is not throwing the error, is there something i am missing here ?

Here is my class

class A {

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    private SimpleJdbcCall simpleJdbcCall;
     
    public int mymeth(){
         simpleJdbcCall.withProcedureName("myproc").declareParameters(new SqlParameter("ID", 
         Types.VARCHAR);
         SqlParameterSource namedParameters = new MapSqlParameterSource("id" , 12);
         Map<String, Object> result = null;
         try {
            //I want Junit to throw error here
            result = simpleJdbcCall.execute(namedParameters);
             
          } catch (Exception e) {
            
           throw new Exception(e.getMessage());
          }
 return (Integer) result.get("Status");
     }

}

here is my Junit Class

@RunWith(SpringRunner.class)

@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Class ATest{
   
  @Autowired
  private A obj;

  @Test
    public void throwExceptionFromMethod() {


       
        try {
            SimpleJdbcCall simpleJdbcCall = Mockito.mock(SimpleJdbcCall.class);
            SqlParameterSource sqlParameterSource = Mockito.mock(SqlParameterSource.class);
           

            Mockito.doThrow(new RuntimeException()).when(simpleJdbcCall ).execute((Object) 
            Mockito.any());
            final int message = obj.mymeth(modifyLeadDispositionRequest);
            Assert.assertEquals(0, message);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Answer

When writing spring-boot integration test you should inject the mock beans using @MockBean annotation

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Class ATest {

    @Autowired
    private A obj;

    @MockBean
    private SimpleJdbcCall simpleJdbcCall;

    @Test
    public void throwExceptionFromMethod() {


   
    try {
       
        Mockito.when(simpleJdbcCall.withProcedureName(Mockito.anyString())).thenReturn(simpleJdbcCall);
        Mockito.when(simpleJdbcCall.declareParameters(Mockito.any())).thenReturn(simpleJdbcCall);
        Mockito.doThrow(new RuntimeException()).when(simpleJdbcCall).execute( 
        ArgumentMatchers.any(SqlParameterSource.class)); 

        //or you can use thenThrow also

        Mockito.when(simpleJdbcCall.execute(ArgumentMatchers.any(SqlParameterSource.class))).thenThrow(RuntimeException.class);

        final int message = obj.mymeth(modifyLeadDispositionRequest);
        
    } catch (Exception e) {
        e.printStackTrace();
       // just to assert here 
    }
  }
}

You can a follow some of the examples here for testing exceptions in junit4 or junit5



Source: stackoverflow