我移植一個應用程序對面JDBC/REST來spring-data-rest
和我唯一的單元測試與失敗,出現錯誤無法啓動彈簧數據休息單元測試,RepositoryRestResource不適用於@Autowire
NoSuchBeanDefinitionException:
No qualifying bean of type 'com.xxx.repository.ForecastRepository' available
該應用程序剛剛裝上了spring-boot
,現在我試圖在spring-data-jpa
的頂部添加spring-data-rest
的新圖層。
我試圖根據
Custom Test Slice with Spring Boot 1.4
制定出正確的Java的配置,但我不得不從慣用的做法偏離,因爲
- 的
@WebMvcTest
註釋不抑制導致測試失敗的安全模塊 @MockMvcAutoConfiguration
由於缺少相關性而失敗,除非我指定@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
(見here)@WebMvcTest
和@SpringBootTest
是相互排斥的,因爲它們都指定@BootstrapWith
而無法運行起來
所以這是我已經有了,但春天無法找到我的@RepositoryRestResource
庫最接近:
庫
@RepositoryRestResource(collectionResourceRel = "forecasts", path = "forecasts")
public interface ForecastRepository extends CrudRepository<ForecastExEncoded,
Long> {
JUnit測試
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK,
classes = {TestRestConfiguration.class})
public class ForecastRestTests {
@Autowired
private MockMvc mockMvc;
@Autowired
private ForecastRepository forecastRepository;
@Before
public void deleteAllBeforeTests() throws Exception {
forecastRepository.deleteAll();
}
@Test
public void shouldReturnRepositoryIndex() throws Exception {
mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk()).andExpect(
jsonPath("$._links.forecasts").exists());
}
}
配置
@OverrideAutoConfiguration(enabled = false)
@ImportAutoConfiguration(value = {
RepositoryRestMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
WebMvcAutoConfiguration.class,
MockMvcAutoConfiguration.class,
MockMvcSecurityAutoConfiguration.class
})
@Import({PropertySpringConfig.class})
public class TestRestConfiguration {}
也試過...
我想配置只有@WebMvcTest
進行單元測試,低於這個@ComponentScan
從How to exclude AutoConfiguration from Spring Boot)試圖簡化它,但excludeFilters
沒有FECT。
@ComponentScan(
basePackages="com.xxx",
excludeFilters = {
@ComponentScan.Filter(type = ASSIGNABLE_TYPE,
value = {
SpringBootWebApplication.class,
JpaDataConfiguration.class,
SecurityConfig.class
})
})
我設置Spring的記錄跟蹤,因爲我只能在這一點上做的是試圖找到線索,有的從日誌輸出發生。目前爲止,雖然沒有任何運氣。
我可以在日誌中看到RepositoryRestConfiguration正在加載,但很明顯它沒有提供正確的信息,我無法在Google文檔和API上搜索並瀏覽完成後做出如何完成。我想我必須在這裏閱讀關於SO的所有相關問題。
更新二○一六年十一月一十六日10:00
有一件事我在我有關的日誌中看到是這樣的:
Performing dependency injection for test context [[email protected] [snip...]
classes = '{class com.xxx.TestRestConfiguration,
class com.xxx.TestRestConfiguration}',
即上下文列出了配置類兩次。我在@SpringBootTest#classes
註釋中指定了配置類(僅限一次)。但是,如果我從註釋中忽略#classes
,Spring Boot將通過@SpringBootApplication
類找到並拉入所有配置。
那麼,這個,我指定在錯誤的地方配置的暗示?我還能怎麼做呢?