Skip to content

Commit

Permalink
Fixtures torn down in reverse order
Browse files Browse the repository at this point in the history
  • Loading branch information
jcechace committed Mar 22, 2022
1 parent 15d5212 commit 324151a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/fixture5/annotations/processors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal class FixtureProcessor(fixtures: List<Fixture>, val store: Store) {
* @throws [FixtureException] if error occurs
*/
fun teardownFixtures() {
fixtureObjects.forEach(this::teardownFixture)
fixtureObjects.asReversed().forEach(this::teardownFixture)
}

private fun setupFixture(type: KClass<out TestFixture>): TestFixture {
Expand Down
24 changes: 20 additions & 4 deletions src/test/kotlin/fixture5/FixtureProcessorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.api.extension.ExtensionContext
import kotlin.test.assertContains
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
import kotlin.test.*

@ExtendWith(MockKExtension::class)
class FixtureProcessorTest {
Expand Down Expand Up @@ -81,4 +78,23 @@ class FixtureProcessorTest {
processor.teardownFixtures()
assertFalse("Fixture object should be torn down") { fixtureObject.initialised }
}

@Test
fun `should setup fixtures in order`() {
val fixtures = listOf(Fixture(CountingSetupFixture::class), Fixture(CountingSetupFixture::class))
val processor = FixtureProcessor(fixtures, store)
processor.setupFixtures()

assertContentEquals(listOf(0, 1), CountingSetupFixture.list.map { it.num })
}

@Test
fun `should tear down fixtures in reverse order`() {
val fixtures = listOf(Fixture(CountingTeardownFixture::class), Fixture(CountingTeardownFixture::class))
val processor = FixtureProcessor(fixtures, store)
processor.setupFixtures()
processor.teardownFixtures()

assertContentEquals(listOf(1, 0), CountingTeardownFixture.list.map { it.num })
}
}
58 changes: 39 additions & 19 deletions src/test/kotlin/fixture5/fixtures.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package fixture5

import fixture5.annotations.FixtureContext
import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.api.extension.ExtensionContext.Store
import kotlin.properties.Delegates

data class TestData(val value: Int = 42)

class ExampleFixture(store: ExtensionContext.Store): TestFixture(store) {
class ExampleFixture(store: Store) : TestFixture(store) {
var initialised by Delegates.notNull<Boolean>()

override fun setup() {
Expand All @@ -19,44 +19,64 @@ class ExampleFixture(store: ExtensionContext.Store): TestFixture(store) {
}

@FixtureContext(provides = [TestData::class])
class ExampleProviderFixture(store: ExtensionContext.Store) : TestFixture(store) {
class ExampleProviderFixture(store: Store) : TestFixture(store) {
override fun setup() {
store(TestData())
}

override fun teardown() {
// no-op
}
override fun teardown() {}
}


@FixtureContext(requires = [TestData::class])
class ExampleConsumerFixture(store: ExtensionContext.Store) : TestFixture(store) {
override fun setup() {
// no-op
}
class ExampleConsumerFixture(store: Store) : TestFixture(store) {
override fun setup() {}

override fun teardown() {
// no-op
}
override fun teardown() {}
}

class ExampleSetupThrowingFixture(store: ExtensionContext.Store) : TestFixture(store) {
class ExampleSetupThrowingFixture(store: Store) : TestFixture(store) {
override fun setup() {
throw RuntimeException("Oopsie Daisy!")
}

override fun teardown() {}
}

class ExampleTeardownThrowingFixture(store: Store) : TestFixture(store) {
override fun setup() {}

override fun teardown() {
// no-op
throw RuntimeException("Oopsie Daisy!")
}
}

class ExampleTeardownThrowingFixture(store: ExtensionContext.Store) : TestFixture(store) {
class CountingSetupFixture(store: Store) : TestFixture(store) {
companion object {
val list = mutableListOf<CountingSetupFixture>()
private var counter = 0
}

val num = counter++

override fun setup() {
// no-op
list.add(this)
}

override fun teardown() {}
}

class CountingTeardownFixture(store: Store) : TestFixture(store) {
companion object {
val list = mutableListOf<CountingTeardownFixture>()
private var counter = 0
}

val num = counter++

override fun setup() {}

override fun teardown() {
throw RuntimeException("Oopsie Daisy!")
list.add(this)
}
}
}

0 comments on commit 324151a

Please sign in to comment.