spring-boot@webmvctest与@springboottest

jmo0nnb3  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(456)

我有一个简单的健康控制器,定义如下:

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Value("${spring.application.name}")
    String serviceName;

    @GetMapping("/health")
    String getHealth() {
        return serviceName + " up and running";
    }
}

以及测试它的测试类:

@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.

这里是 AppConfiguration.java 在与主spring boot应用程序类相同的包中定义:

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties
public class AppConfiguration {

    @Value("${redis.host}")
    private String redisHost;

    @Value("${redis.port}")
    private int redisPort;

    @Value("${redis.password:}")
    private String redisPassword;
...
// getters and setters come here

主要类别:

@SpringBootApplication
public class LiveDataServiceApplication {

    @Autowired
    private AppConfiguration configuration;

    public static void main(String[] args) {
        SpringApplication.run(LiveDataServiceApplication.class, args);
    }

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        RedisStandaloneConfiguration redisConfiguration = new RedisStandaloneConfiguration(configuration.getRedisHost(), configuration.getRedisPort());
        redisConfiguration.setPassword(configuration.getRedisPassword());
        return new LettuceConnectionFactory(redisConfiguration);
    }
}

如果我在test类中修改注解如下,则测试通过:

@SpringBootTest
@AutoConfigureMockMvc
class AdminControllerTest {
....

我错过了什么?

bvjxkvbb

bvjxkvbb1#

你应该了解 @WebMvcTest 以及 @SpringBootTest @webmvctest:注解只是示例化了web层而不是整个上下文,所以所有的依赖关系都应该在controller类中进行模拟,你可以看看文档
springboot只示例化web层,而不是整个上下文。在一个有多个控制器的应用程序中,您甚至可以通过以下方式请求只示例化一个控制器,例如, @WebMvcTest(HomeController.class). 我们使用@mockbean为greetingservice创建并注入一个mock(如果不这样做,应用程序上下文将无法启动)
springboottest:springboottest注解实际加载测试环境的应用程序上下文
@springboottest注解告诉spring boot查找一个主配置类(例如带有@springbootapplication的配置类),并使用它来启动spring应用程序上下文。

zf2sa74q

zf2sa74q2#

在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());

    }
    }

相关问题