我正在使用Kotlin、Spring和Spek实现简单的微服务。我想测试我的存储库,但我想知道如何将repo注入spek测试用例。每个示例或教程都基于创建新的参考,如下所示:
object SampleTest : Spek({
describe("a calculator") {
val calculator = SampleCalculator()
it("should return the result of adding the first number to the second number") {
val sum = calculator.sum(2, 4)
assertEquals(6, sum)
}
it("should return the result of subtracting the second number from the first number") {
val subtract = calculator.subtract(4, 2)
assertEquals(2, subtract)
}
}
})总之,我不想做这样的事情:
val calculator = SampleCalculator()我想要实现这一点
@Autowired
val calculator: SampleCalculator但我不能这样做,因为我不能将服务自动连接到本地变量中。有什么解决方案吗?我对kotlin和spek是新手。
发布于 2017-11-25 18:53:20
在lateinit上试试:
@Autowired
lateinit var calculator: SampleCalculator发布于 2019-01-16 00:59:13
看看GitHub上的spek-spring-extension项目,有一种方法可以从Spring context注入bean:
用于Spek的Spring扩展
这是在Spek中编写spring集成测试的概念证明
限制
目前仅支持注入bean。
@ContextConfiguration(classes = arrayOf(MyConfiguration::class))
object MySpec: Spek({
val context = createContext(MySpec::class)
val foo = context.inject<Foo>()
// val foo: Foo by context.inject()
it("blah blah blah") {
foo.doSomething()
}
})问题
Spring的TestContext框架对测试的结构进行了假设,这与Spek不兼容,这意味着我们不能使用TestContextManager (我们可以,但它将非常黑客)。
https://stackoverflow.com/questions/42064001
复制相似问题