@Mockbean возвращает null объектJAVA

Программисты JAVA общаются здесь
Anonymous
@Mockbean возвращает null объект

Сообщение Anonymous »

Я пытаюсь использовать @mockbean; Java версия 11, версия Spring Framework (5.3.8), версия Spring Boot (2.5.1) и Junit Jupiter (5.7.2). < /p>
@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", "");
}
}
< /code>
@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);
...
}
}
< /code>
attachmentService is not being mocked it gives null in return

Подробнее здесь: https://stackoverflow.com/questions/687 ... ull-object

Вернуться в «JAVA»