Java SpringBoot@WebMvcTest与@SpringBootTest
我有一个简单的健康控制器,定义如下:Java SpringBoot@WebMvcTest与@SpringBootTest,java,spring-boot,spring-test-mvc,Java,Spring Boot,Spring Test Mvc,我有一个简单的健康控制器,定义如下: @RestController @请求映射(“/admin”) 公共类管理员控制器{ @值(${spring.application.name}”) 字符串serviceName; @GetMapping(“/health”) 字符串getHealth(){ 返回serviceName+“启动并运行”; } } 以及测试它的测试类: @WebMvcTest(RedisController.class) class AdminControllerTest {
@RestController
@请求映射(“/admin”)
公共类管理员控制器{
@值(${spring.application.name}”)
字符串serviceName;
@GetMapping(“/health”)
字符串getHealth(){
返回serviceName+“启动并运行”;
}
}
以及测试它的测试类:
@WebMvcTest(RedisController.class)
class AdminControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void healthShouldReturnDefaultMessage() throws Exception {
this.mockMvc.perform(get("/admin/health"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("live-data-service up and running")));
}
}
运行测试时,我得到以下错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field configuration in com.XXXX.LiveDataServiceApplication required a bean of type 'com.XXXXX.AppConfiguration' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.XXXX.AppConfiguration' in your configuration.
以下是在与主spring boot应用程序类相同的包中定义的AppConfiguration.java
:
@配置
@EnableConfigurationProperties
@配置属性
公共类应用程序配置{
@值(${redis.host}”)
私有字符串主机;
@值(${redis.port}”)
私人int再报告;
@值(${redis.password:}”)
私有字符串密码;
...
//能手和二传手都来了
主要类别:
@springboot应用程序
公共类LiveDataServiceApplication{
@自动连线
私有应用程序配置;
公共静态void main(字符串[]args){
run(LiveDataServiceApplication.class,args);
}
@豆子
公共RedisConnectionFactory RedisConnectionFactory(){
RedisStandaloneConfiguration redisConfiguration=新的RedisStandaloneConfiguration(configuration.getRedisHost(),configuration.getRedisPort());
redisConfiguration.setPassword(configuration.getRedisPassword());
返回新的LettuceConnectionFactory(重新配置);
}
}
如果我在测试类中修改注释如下,则测试通过:
@SpringBootTest
@AutoConfigureMockMvc
类管理员测试{
....
我缺少什么?在src/test/resource/application.file中定义所有属性 将junit 5用于rest层的示例:
@ExtendWith(MockitoExtension.class)
public class RestTest {
@InjectMocks
private RestClass restClass;
private MockMvc mockMvc;
@BeforeEach
public void init() throws Exception {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(restClass).build();
}
@Test
public void test() throws Exception {
String url = "/url";
ResultActions resultActions = mockMvc.perform(get(url));
resultActions.andExpect(status().isOk());
}
}
您应该了解
@WebMvcTest
和@springbootest
@WebMvcTest:注释只是实例化web层,而不是整个上下文,因此应该模拟控制器类中的所有依赖项,您可以查看
Spring Boot仅实例化web层,而不是整个上下文。在具有多个控制器的应用程序中,您甚至可以通过使用例如@WebMvcTest(HomeController.class)来请求仅实例化一个控制器。
我们使用@MockBean为GreetingService创建并注入一个mock(如果不这样做,应用程序上下文将无法启动)
SpringBootTest:SpringBootTest注释实际加载测试环境的应用程序上下文
@SpringBootTest注释告诉Spring Boot查找主配置类(例如带有@SpringBootApplication的配置类),并使用该配置类启动Spring应用程序上下文
您正在
LiveDataServiceApplication
类中为AppConfiguration
进行自动连线。但是您不必将AppConfiguration
类作为bean进行营销。我没有理解您的意思,您不必将AppConfiguration类作为bean进行营销是什么意思?您需要使用任何stere注释该类AppConfiguration
像@Service
这样的otype注释我已经用@Configuration
定义了它,它的别名是@Component
。这还不够吗?不@Configuration
不是@Component
的别名。两者都有不同的用途和目的。那么,为什么我的AppConfiguration
类在我开始时被加载对应用程序进行艺术处理,但不是在我运行测试时进行艺术处理?我真的应该保留一个单独的配置类吗?一旦它被删除并替换在LiveDataServiceApplication
中读取的属性,它在测试中也能工作。我认为如果你将它们移动到单独的包中,它将无法工作,请阅读我的答案和附加文档@belgorosNo,I moved nothing,只是删除了AppConfiguration类如果您删除它会起作用,因为它不需要将AppConfiguration
注入到主类中,当您说@WebMvcTest
时,它只会加载控制器的,所有其他bean都应该是mockedOk,是否有关于使用单独配置类的文档?或者我们可以声明任何文档无论项目中的何处,都需要带有«@Value»注释的属性?