2017-07-18 73 views
0

Spring Boot版本1.5.4.RELEASE。Spring Boot單元測試 - 缺少運行測試的豆

運行應用程序正常工作正常。

當運行單元測試,一些豆子缺失,例如:

*************************** 
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招搖UI休息文件提出。刪除此應用程序依賴項時,測試會成功完成。

我在這裏錯過了什麼?在單元測試「環境」中找不到或自動配置一些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 
    .... 
} 

這是唯一的測試控制器我現在有。

+0

看來你已經有了不同的配置來啓動應用程序和測試。你可以分享請通過Git你的應用程序?我需要檢查你的配置,沒有這些信息,我無法爲你提供修復。只有建議檢查您的配置(配置文件,帶註釋的上下文配置...)。 – Sergii

+0

這是一個私人項目,但我真的沒有特別的測試或其他配置。大部分是使用Spring Boot自動配置的。請參閱更新部分以進行測試。 – xDs

+1

您必須使用@SpringBootTest(classes = Application.class)註釋您的ProfileControllerTest類以加載測試的測試上下文。 –

回答

0

@SpringBootTest(classes = Application.class)作爲ProfileControllerTest的註釋。

相關問題