代码之家  ›  专栏  ›  技术社区  ›  SpringLearner

Junit mocking Context.class不工作

  •  0
  • SpringLearner  · 技术社区  · 2 年前

    我有springboot 2.7运行项目和junit jupiter 5.8.2。 在为同样的问题编写Junit时,代码运行得非常好。 嘲笑TemplateEngine时 when(templateEngine.process(anyString(), any(Context.class))).thenReturn("Processed template");

    我得到InvalidUseOfMatcherException

    我不能使用我不能在组织中使用的力量mockito。

    
    public void sendEmail(File f, String name) throws MessagingException {
    Context contaxt = new Context();
    context.setVariable(“X”, 1); 
    context.setVariable("Y", 2);
    // Process the Thymeleaf template
    String emailContent = templateEngine process(“email-template", context);
    MimeMessage message = emailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setFrom(“[email protected]”);
    helper.setTo(fileName.equals(“x.csv”) ? “[email protected]” : “[email protected]”);
    helper.setCc(“[email protected]”);
    helper.setSubject("Repo“); 
    if(!fileName.equals(acceptFileName)){
    helper.setText(emailContent,true);
    }
    else {
    helper.setText("New Details");
    }
    helper.addAttachment (name, f); 
    emailSender.send(message);
    }
    
    

    我试过这样做,我得到了一个不能模拟/间谍的最后一堂课

           // Mocked email content
            String mockedEmailContent = "Mocked email content";
            when(templateEngine.process(eq("email-template"), any(Context.class))).thenReturn(mockedEmailContent);
    

    有办法解决这个问题吗

    0 回复  |  直到 2 年前