簡體   English   中英

如何替換@ContextConfiguration (Java Spring) 上的@SpringBootTest?

[英]How to replace @SpringBootTest on @ContextConfiguration (Java Spring)?

我有測試 class,@SpringBootTest:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnviroment = SpringBootTest.WebEnviroment.RANDOM_PORT, classes = App.class)
public class MyTest {
...
}

我需要替換@ContextConfiguration 上的@SpringBootTest,我這樣做了:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "my.xml", initializers = MyTest.MockJee.class)
public class MyTest {
...
}

但是@SpringBootTest 有參數,@ContextConfiguration 沒有:

webEnviroment = SpringBootTest.WebEnviroment.RANDOM_PORT

因此,如果我可以加入@SpringBootTest 和@ContextConfiguration,那就太好了。 但是 SpringBootTest + ContextConfiguration 會產生沖突(它們都會創建上下文),所以這就是我想替換 SpringBootTest 的原因。 問題:如何制作這樣的:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "my.xml", initializers = MyTest.MockJee.class, webEnviroment = SpringBootTest.WebEnviroment.RANDOM_PORT)
public class MyTest {
...
}

這實際上取決於您要實現的目標。 但是使用SpringBootTest.WebEnviroment.RANDOM_PORT的效果之一是實際將屬性server.port設置為 0。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM