简体   繁体   English

Junit 强制在方法调用上抛出异常

[英]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?每当调用simpleJdbcCall.execute(namedParameters)时,我都会尝试抛出异常,但我看到它没有抛出错误,这里有什么我遗漏的吗?

Here is my class这是我的 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这是我的 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();
        }
    }
}

When writing spring-boot integration test you should inject the mock beans using @MockBean annotation在编写 spring-boot 集成测试时,您应该使用@MockBean注释注入模拟 bean

@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您可以按照此处的一些示例来测试junit4junit5中的异常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM