В новой версии KotlinTest 3.3 появились новые Слушатели для определения beforeTest
, afterTest
, beforeSpec
и т. Д.
В этом случае возможное решение должноbe:
class BlockchainUT : DescribeSpec(), TestListener {
var blockchain = Blockchain()
override fun beforeTest(describe: TestCase): Unit {
blockchain = Blockchain()
blockchain.addBlock(listOf("foo1", "bar1"))
blockchain.addBlock(listOf("foo2", "bar2"))
blockchain.addBlock(listOf("foo3", "bar3"))
}
init {
describe("isValidChain()") {
context("when the chain starts with the genesis block and has multiple blocks") {
context("and a lastHash reference has changed returns false") {
blockchain.chain[2] = Block(
blockchain.chain[2].timestamp,
"broken-lastHash",
blockchain.chain[2].hash,
blockchain.chain[2].data)
it("returns false") {
Blockchain.isValid(blockchain) shouldBe false
}
}
context("and the chain contains a block with an invalid field") {
blockchain.chain[2] = Block(
blockchain.chain[2].timestamp,
blockchain.chain[2].lastHash,
blockchain.chain[2].hash,
listOf("some-bad-and-evil-data"))
it("returns false") {
Blockchain.isValid(blockchain) shouldBe false
}
}
context("and the chain does not contain any invalid blocks") {
it("returns true") {
Blockchain.isValid(blockchain) shouldBe true
}
}
}
}
}
}
Также может быть решена с помощью новых Режимов изоляции , определяющих режим изоляции на InstancePerLeaf
.От io.kotlintest.IsolationMode
Javadoc:
A new instance of the [Spec] class is instantiated for every
[TestCase] - both containers and leaf tests - and they are
executed once the previous test has completed.
For example, in the following test plan:
"this test" {
println("a")
"nested test" {
println("b")
}
"nested test 2" {
println("c")
}
}
The output will be:
a
a
b
a
c
И, наконец, переместите код, который вы хотите повторить для каждого [TestCase]
class BlockchainUT : DescribeSpec(){
override fun isolationMode() = IsolationMode.InstancePerTest
describe("isValidChain()") {
context("when the chain starts with the genesis block and has multiple blocks") {
var blockchain = Blockchain()
blockchain.addBlock(listOf("foo1", "bar1"))
blockchain.addBlock(listOf("foo2", "bar2"))
blockchain.addBlock(listOf("foo3", "bar3"))
context("and a lastHash reference has changed returns false") {
blockchain.chain[2] = Block(
blockchain.chain[2].timestamp,
"broken-lastHash",
blockchain.chain[2].hash,
blockchain.chain[2].data)
it("returns false") {
Blockchain.isValid(blockchain) shouldBe false
}
}
context("and the chain contains a block with an invalid field") {
blockchain.chain[2] = Block(
blockchain.chain[2].timestamp,
blockchain.chain[2].lastHash,
blockchain.chain[2].hash,
listOf("some-bad-and-evil-data"))
it("returns false") {
Blockchain.isValid(blockchain) shouldBe false
}
}
context("and the chain does not contain any invalid blocks") {
it("returns true") {
Blockchain.isValid(blockchain) shouldBe true
}
}
}
}
}
}