【问题标题】:Junit Force to throw Exception on method callJunit强制在方法调用上抛出异常
【发布时间】:2021-07-17 19:04:08
【问题描述】:

每当 simpleJdbcCall.execute(namedParameters) 调用时,我都会尝试抛出异常,但我发现它没有抛出错误,这里有什么我遗漏的吗?

这是我的课

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

}

这是我的 Junit 课程


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

【问题讨论】:

  • 你的 obj 得到了 SpringRunner 注入的 SimpleJdbcCall,你的 mock 永远不会被使用
  • @Turo,你能分享任何例子吗?

标签: java spring spring-boot junit spring-boot-test


【解决方案1】:

在编写 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 
    }
  }
}

您可以按照此处的一些示例来测试junit4junit5 中的异常

【讨论】:

  • 我在上面尝试过,但在 A 类中的 simpleJdbcCall.withProcedureName("myproc") 处得到 NULL
  • @user1591156 你也必须模拟这个调用。编辑答案
  • @Deadpool 希望你不介意,为那个感觉不对的自己做一个答案:-)
  • Deadpool 和 Turo 感谢您的意见。
猜你喜欢
  • 2012-11-22
  • 1970-01-01
  • 2017-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-08
相关资源
最近更新 更多