Spring 引导单元测试 - 缺少 bean 运行测试



Spring Boot version 1.5.4.RELEASE.

正常运行应用程序。

运行单元测试时,缺少某些 bean,例如:

***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in ...service.post.PostService required a bean named 'mongoTemplate' that could not be found.

Action:
Consider defining a bean named 'mongoTemplate' in your configuration.

我正在使用MongoDB存储库。我通过添加一个返回此 bean 的配置类来解决此问题。

在此之后,我尝试再次执行单元测试,出现以下错误:

***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in springfox.documentation.spring.data.rest.BasePathAwareServicesProvider required a bean of type 'org.springframework.data.rest.core.config.RepositoryRestConfiguration' that could not be found.

Action:
Consider defining a bean of type 'org.springframework.data.rest.core.config.RepositoryRestConfiguration' in your configuration.

这是由 springfox swagger ui Rest 文档提出的。删除此应用程序依赖项时,测试将成功完成。

我在这里错过了什么?某些 bean 无法在单元测试"环境"中找到或自动配置。

更新

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ApplicationTests {
@Test
public void contextLoads() {}
}
@RunWith(SpringRunner.class)
@WebMvcTest(value = ProfileController.class, secure = false)
public class ProfileControllerTest {
private MockMvc mockMvc;
@MockBean private ProfileService profileService;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders.standaloneSetup(new ProfileController(profileService)).build();
}
@Test
....
}

这是我目前拥有的唯一测试控制器。

修改代码以显示如何排除运行单元测试时通常需要的配置:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ApplicationTests {
@Test
public void contextLoads() {}
}
@RunWith(SpringRunner.class)
@WebMvcTest(value = ProfileController.class, secure = false, excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class) // or any other configuration.
public class ProfileControllerTest {
private MockMvc mockMvc;
@MockBean private ProfileService profileService;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders.standaloneSetup(new ProfileController(profileService)).build();
}
@Test
....

}

您可以在此处找到包含集成和单元测试的完整 Spring 启动应用程序。

@SpringBootTest(classes = Application.class( 作为 ProfileControllerTest 的注释。

我在 SpringBoot 版本上遇到了类似的问题:2.2.5.RELEASE 这就是我的问题解决的方式。

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(classes = Application.class)
public class ApplicationTests {
@Test
public void contextLoads() {}
}
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
....
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class ProfileControllerTest {
private MockMvc mockMvc;
@InjectMocks
private ProfileController profileController;
@InjectMocks
private PageableHandlerMethodArgumentResolver pageableArgumentResolver;
@BeforeEach
public void setup() {
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders.standaloneSetup(profileController).setCustomArgumentResolvers(pageableArgumentResolver).build();
}
@Test
....
}

相关内容

最新更新