我在我的MyService.kt.
通過和useXml讀取application.ymlapplication-test.yml
@Service
class MyService (
@Value("\${something.use-xml}")
var useXml: Boolean = false
) {
if(useXml) {
// do this
} else {
// do that
}
....
}
這按預期作業。
但對于測驗,我想同時使用這兩種變體:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class MyFunctionalTest {
@Value("\${something.use-xml}")
var useXml: Boolean = false
//....
@ParameterizedTest
@ValueSource(booleans = [true,false])
fun `do a parameterized test`(useXML : Boolean) {
useXml = useXML // this is (of course) not setting the variations for `MyService.kt`
if (useXML) {
// test this
} else {
// test that
}
}
我想要一種可以為application.yml檔案設定變數的可能性,因此對兩種變體都進行了測驗。
有什么辦法嗎?
uj5u.com熱心網友回復:
當然,這是可能的。您需要注入您正在測驗的服務并設定其useXml值。在你的情況下,它看起來像這樣:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
internal class MyServiceTest {
@Autowired
private lateinit var myService: MyService
@ParameterizedTest
@ValueSource(booleans = [true,false])
fun `do a parameterized test`(useXML : Boolean) {
myService.useXml = useXML // you have to set the value for the test case
Assertions.assertEquals(useXML, myService.useXml)
}
}
但是,如果我是你并且有這樣的情況,我會使用同一個介面的兩個完全獨立的實作,如果xml和其他格式有共同點。最后,你將有兩個類(你需要更好的命名):
MyServiceXmlMyServiceJson
這兩個類都將被注釋@ConditionalOnProperty("\${something.use-xml}"),它們將實作相同的介面。
這將很容易測驗,兩個不同的實作,所以兩個不同的測驗類。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/524819.html
上一篇:Kotlin和Spring-Flow與Suspend
下一篇:java.sql.SQLException:Parameterindexoutofrange(2>numberofparameters,whichis1)錯誤
