使用Mockito-mock,void方法

示例

public Response date( String url) {
    @Resource
    private IRuleJudgmentService ruleJudgmentService;
    
    //示例方法,伪代码
    ruleJudgmentService.ruleJudgement(参数, 参数);
    //其他业务 
    
    return Response;
}

调用的void方法

public interface IRuleJudgmentService {
    //示例,伪代码
    void ruleJudgement(参数,参数);
}

Test方法编写

//自己要mock的类
public class AnalysisServiceImplTest extends BaseMockitoTest {
    //本方法
    @InjectMocks
    private AnalysisServiceImpl analysisService;
    
    //注入mock的接口
    @Mock
    private IRuleJudgmentService ruleJudgmentService;
    
    @Test
    public void dateTest() {
        
        String url = "url";
        /**
        *  anyLong()方法参数占位,你的参数是什么类型就用什么来占位
        *  比如我这里的参数是ruleJudgement("参数", "参数");
        *  这里就是ruleJudgement(anyString(), anyString());
        **/
        doNothing().when(ruleJudgmentService).ruleJudgement(anyLong(), anyLong());
        
        //调用本方法,将设置好的参数放到方法中
         Response response = analysisService.date(url);
        
        //验证
        assertThat(response.getMessage(),is("成功[OK]"));
    }
    
}

 

posted @ 2023-04-28 11:06  Dshzs月  阅读(318)  评论(0编辑  收藏  举报