繁体   English   中英

Spring Boot 单元测试 - 缺少 bean 运行测试

[英]Spring Boot Unit Test - Missing beans running test

Spring Boot 版本 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 Boot 应用程序。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM