7

I am trying to use @MockBean; java version 11, Spring Framework Version (5.3.8), Spring Boot Version(2.5.1) and Junit Jupiter (5.7.2) .

 @SpringBootTest
 public class PostEventHandlerTest {
 @MockBean
 private AttachmentService attachmentService;
 @Test
 public void handlePostBeforeCreateTest() throws Exception {
 Post post = new Post("First Post", "Post Added", null, null, "", "");
 
 Mockito.when(attachmentService.storeFile("abc.txt", "")).thenReturn(new Attachment());
 
 PostEventHandler postEventHandler = new PostEventHandler();
 postEventHandler.handlePostBeforeCreate(post);
 verify(attachmentService, times(1)).storeFile("abc.txt", "");
 }
 }
 @Slf4j
 @Component
 @Configuration
 @RepositoryEventHandler
 public class PostEventHandler {
 @Autowired
 private AttachmentService attachmentService;
 @Autowired
 private PostRepository postRepository;
 public void handlePostBeforeCreate(Post post) throws Exception {
 ...
 /* Here attachmentService is found null when we execute above test*/
 attachmentService.storeFile(fileName, content);
 ...
 }
 }

attachmentService is not being mocked it gives null in return

asked Aug 9, 2021 at 5:39
1
  • 2
    How’s the mocked attachment service suppose to get into your postEventHandler? You have to use Spring‘s mechanisms to create the handler, otherwise it won’t be able to inject mocks at the applicable places. Commented Aug 9, 2021 at 16:41

2 Answers 2

0

I met a similar problem: I also had null in mocked bean, but only when I run several tests at once (for example, when I run "mvn clean package")

If it was your case (or if it case of someone, who will see this post), then this situation might be solved by annotation @DirtiesContext on every test class youre running

Eric Aya
70.2k36 gold badges190 silver badges266 bronze badges
answered Jul 18, 2022 at 9:51
Sign up to request clarification or add additional context in comments.

Comments

0

As @johanneslink said, the problem is this line:

PostEventHandler postEventHandler = new PostEventHandler();

Spring won't inject anything into your PostEventHandler bean if you manually construct it.

The following should make your test work, note the @Autowired postEventHandler. :)

@SpringBootTest
public class PostEventHandlerTest {
 @MockBean
 private AttachmentService attachmentService;
 @Autowired
 PostEventHandler postEventHandler;
 
 @Test
 public void handlePostBeforeCreateTest() throws Exception {
 
 Post post = new Post("First Post", "Post Added", null, null, "", "");
 
 Mockito.when(attachmentService.storeFile("abc.txt", "")).thenReturn(new Attachment());
 postEventHandler.handlePostBeforeCreate(post);
 
 verify(attachmentService, times(1)).storeFile("abc.txt", "");
 }
}
answered Jul 19, 2022 at 19:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.