Mock call object method (Mockito Junit5 Spring)

I try to mock the thymeleaf template engine.

My test class:

@ExtendWith(MockitoExtension.class) @RunWith(JUnitPlatform.class) public class MailServiceTest {     @InjectMocks     private MailService mailService;          @Mock private JavaMailSender mailSender;     @Mock private TemplateEngine templateEngine;          @BeforeEach     public void setMockOutput() {         doReturn("<html>Template processed String</html>").when(templateEngine.process(any(String.class), any(IContext.class)));     }      @Test     public void testNewMailBoxMail() throws MessagingException, IOException {                          mailService.newMailBoxMail("M.", "nom", "[email protected]");          ...     } 

I got a lot of trouble with the process method parameter (org.mockito.exceptions.misusing.InvalidUseOfMatchersException) and I’m not sure that they are OK.

As the test reach the doReturn line I get

java.lang.IllegalArgumentException: Template cannot be null     at org.thymeleaf.util.Validate.notNull(Validate.java:37)     at org.thymeleaf.TemplateSpec.<init>(TemplateSpec.java:314)     at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1048)     at fr.strasbourg.accountManagement.service.MailServiceTest.setMockOutput(MailServiceTest.java:50)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:498)     at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:515)     at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)     at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.invokeMethodInExtensionContext(ClassTestDescriptor.java:436)     at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$synthesizeBeforeEachMethodAdapter$14(ClassTestDescriptor.java:424)     at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeEachMethods$2(TestMethodTestDescriptor.java:136)     at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)     at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeMethodsOrCallbacksUntilExceptionOccurs(TestMethodTestDescriptor.java:156)     at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeEachMethods(TestMethodTestDescriptor.java:135)     at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:110)     at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:59)     at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:105)     at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)     at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95)     at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)     at java.util.ArrayList.forEach(ArrayList.java:1249)     at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)     at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:110)     at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)     at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95)     at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)     at java.util.ArrayList.forEach(ArrayList.java:1249)     at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)     at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:110)     at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)     at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95)     at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)     at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)     at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)     at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)     at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)     at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)     at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)     at org.junit.platform.runner.JUnitPlatform.run(JUnitPlatform.java:139)     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)     at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210) 

Can somebody help my to mock Thymeleaf template engine?

Thank Dominique

The template engine is injected as bean in the MailService object. Here is the MailService code:

@Service public class MailService {     @Autowired private JavaMailSender mailSender;     @Autowired private TemplateEngine templateEngine;          private static final String SUPPORT_EMAIL_ADDRESS = "[email protected]";          public void newMailBoxMail(String pCivility, String pName, String pEmailAddress) throws MessagingException {         // template context (model)         final Context vContext = new Context();         vContext.setVariable("civility", pCivility);         vContext.setVariable("name", pName);         vContext.setVariable("email", pEmailAddress);                  // create HTML from template         final String vHtmlContent = templateEngine.process("html/NewMailbox.html", vContext);                  // create mail         MimeMessage vMimeMessage = mailSender.createMimeMessage();                  MimeMessageHelper vMessage = new MimeMessageHelper(vMimeMessage, "UTF-8");         vMessage.setSubject("Charte messagerie");         vMessage.setFrom(SUPPORT_EMAIL_ADDRESS);         vMessage.setTo(pEmailAddress);         vMessage.setText(vHtmlContent, true);                  // send mail         mailSender.send(vMimeMessage);     } } 
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.