在Jenkins中運行時,我的集成測試出現了一些不可思議的情況,它在Jenkins中運行時無法連接主模擬的主Bean。使用@SpringApplicationConfiguration進行集成測試,使用在Jenkins中不工作的模擬
但是,當使用Maven安裝在本地運行時,它可以完美地工作。這是我的集成測試:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {App.class, SampleIT.class})
@WebIntegrationTest({"server.port:0"})
public class SampleIT {
@Primary
@Bean
public SampleRepository sampleRepository() { //this is wired fine in my local Maven, but not in Jenkins...
return Mockito.mock(Sample.class);
}
@Test
public void testSomething {
//when this is executed, the mock has been wired at this point in my local Maven
//however in Jenkins, it still uses the real SampleRepositoryImpl, not the mock
}
}
這個App.class,這是一個春天啓動的應用程序:
@SpringBootApplication
@EnableAspectJAutoProxy
@EnableDiscoveryClient
@ComponentScan("com.sample")
@EnableJpaRepositories("com.sample")
@EntityScan("com.sample")
@DependsOn("OAuth2Config")
public class App extends SpringBootServletInitializer {
public static void main(final String[] args) {
SpringApplication.run(App.class, args);
}
}
我肯定可以看到,詹金斯被使用,而不是模擬真實SampleRepositoryImpl,因爲在Jenkins控制檯中生成的sql語句。與我的本地Maven不同,它沒有生成sqls,因爲它使用的是模擬。
有沒有其他人會遇到這種情況?
如何解決這個問題,讓詹金斯運行也會使用模擬,這被標記爲主要的bean?
--------- --------- UPDATE
如果我:
- 從@SpringApplicationConfiguration 刪除SampleIT.class
- 並添加@配置到SampleIT
它的工作原理,嘲笑是有線的。然而,這變成了一個全局配置,我的其他集成測試開始提取在這個類中定義的模擬。所以它不是一個真正的解決方案,爲什麼@SpringApplicationConfiguration似乎不工作?
仍然在春季啓動1.3,因此沒有@MockBean呢。我正在使用mvn install在我的本地執行junit。 –
您可以嘗試Maven Test,它應該反映出與Jenkins相同的結果。 我在我的項目中也遇到了同樣的問題,Boot正在創建引用原始存儲庫。因此,我已annoted類 '@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.MOCK)' 和回購 '@Mock \t MyRepository myRepository;' 您可以嘗試以上選項。 – sandeshch
不過@SpringBootTest只在Spring Boot 1.4中可用,我們的項目還沒有升級到1.4。 –