@MockBean возвращает нулевой объектJAVA

Программисты JAVA общаются здесь
Anonymous
@MockBean возвращает нулевой объект

Сообщение Anonymous »

Я пытаюсь использовать @MockBean; версия Java 11, версия Spring Framework (5.3.8), версия Spring Boot (2.5.1) и 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 не подвергается насмешкам, он возвращает ноль

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