小编oza*_*oza的帖子

如何在junit Springboot中测试私有方法

我需要测试私有方法。以下方法的正确测试方法是什么?我尝试使用 Mockito 时..但是我如何模拟私有方法。我认为我们不能模拟私有方法。

    private classObject privateMethod(Message message){
        try{
           Objectmapper mapper = new ObjectMapper();
           return mapper.readValue(message.getBody(), ClassName.class);
        }catch(){
           throw new Exception(); 
        }
    }

    //I am getting an exception while testing

    byte[] body = {10,-11,12,14,15};
    MessageProperties msgProp = new MessageProperties();
    Message message = new Message(body, msgProp);

    // the above message is passed as parameter to function through                           
    // which private method is called
    objectxyz.execute(message);

    // execute method
    public void execute(Message message){
        objectxyz xyz = privateMethod(message);
        objectabc abc = service.someMethod(xyz);
        List<object> list = …
Run Code Online (Sandbox Code Playgroud)

java junit mockito spring-boot

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

java ×1

junit ×1

mockito ×1

spring-boot ×1