Я пытаюсь преобразовать тестовый код контроллера Spring Boot из JUnit 4 в JUnit 5. Я почти заменил все аннотации JUnit 4 на JUnit 5, но у меня возникли проблемы при попытке заменить @RunWith
на @ExtendWith
.
@ExtendWith(SpringExtension.class)
@WebMvcTest(value = HappyPostController.class, secure = false)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class HappyPostControllerTest {
@Autowired
private MockMvc mockMvc;
@Mock
private HappyPostServiceImpl happyPostService;
@InjectMocks
private HappyPostController happyPostController;
@BeforeAll
public void initialize() {
happyPostController = new HappyPostController(happyPostService);
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders
.standaloneSetup(happyPostController)
.build();
}
@Test
public void testMockCreation() {
Assertions.assertNotNull(happyPostService);
Assertions.assertNotNull(mockMvc);
}
//..... other test methods
}
Ошибки:
java.lang.IllegalStateException: Failed to load ApplicationContext
......
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'happyPostController' defined in file [....\HappyPostController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.rok.capp.service.HappyPostService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
.....
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.rok.capp.service.HappyPostService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
........
Test ignored.
Я пытался найти решение, но не смог.Пожалуйста, помогите.
Спасибо