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

Posted

技术标签:

【中文标题】Junit强制在方法调用上抛出异常【英文标题】:Junit Force to throw Exception on method call 【发布时间】: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,你能分享任何例子吗? 【参考方案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 
    
  

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

【讨论】:

我在上面尝试过,但在 A 类中的 simpleJdbcCall.withProcedureName("myproc") 处得到 NULL @user1591156 你也必须模拟这个调用。编辑答案 @Deadpool 希望你不介意,为那个感觉不对的自己做一个答案:-) Deadpool 和 Turo 感谢您的意见。

以上是关于Junit强制在方法调用上抛出异常的主要内容,如果未能解决你的问题,请参考以下文章

在wso2上抛出异常

JMockit 期望 API:如何在方法/构造函数调用时抛出异常

glCreateShader 在 OSX 上抛出异常,为啥?

如果项目哈希键已存在,如何让DynamoDbContext在Save上抛出异常

为啥要尝试在已检查的异常上抛出未检查的异常? [复制]

AVAudioRecorder 在 prepareToRecord 上抛出异常