簡體   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