Skip to content

Commit 3740252

Browse files
committed
Generalise bank business logic contract
Extract interface from bank service so that it can express the actions the bank is able to do. This allows to write tests in terms of the contract, showing how the bank is meant to work without coupling them to specific details of a given layer of the application stack. This will make more sense as soon as the http api is implemented. Use gradle test fixtures in order to expose the test to other projects. It's important to notice, that the each layer of the stack can add extra tests that are relevant to its context. The tests in terms of the contract are a baseline and by no means a limitation to the tests that can be written. In the case of the domain, an additional test shows that account creation generates its id.
1 parent 02cb0af commit 3740252

6 files changed

Lines changed: 45 additions & 15 deletions

File tree

domain/build.gradle

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
plugins {
2+
id 'java-test-fixtures'
3+
}
4+
5+
dependencies {
6+
testFixturesImplementation 'org.jetbrains.kotlin:kotlin-test-junit5'
7+
testFixturesImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
8+
testFixturesImplementation 'com.natpryce:hamkrest:1.8.0.1'
9+
}
10+
11+
compileTestFixturesKotlin {
12+
kotlinOptions.jvmTarget = '11'
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package lmirabal.bank
2+
3+
import lmirabal.bank.model.BankAccount
4+
5+
interface Bank {
6+
fun createAccount(): BankAccount
7+
fun listAccounts(): List<BankAccount>
8+
}

domain/src/main/kotlin/lmirabal/bank/BankService.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import lmirabal.bank.model.BankAccountId
88
class BankService(
99
private val accountRepository: BankAccountRepository,
1010
private val idFactory: () -> BankAccountId = { BankAccountId.random() }
11-
) {
12-
fun createAccount(): BankAccount {
11+
) : Bank {
12+
override fun createAccount(): BankAccount {
1313
return BankAccount(idFactory(), Amount.ZERO)
1414
.also { newAccount -> accountRepository.add(newAccount) }
1515
}
1616

17-
fun listAccounts(): List<BankAccount> = accountRepository.list()
17+
override fun listAccounts(): List<BankAccount> = accountRepository.list()
1818
}

domain/src/test/kotlin/lmirabal/bank/BankServiceTest.kt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,15 @@ import lmirabal.bank.model.BankAccount
88
import lmirabal.bank.model.BankAccountId
99
import org.junit.jupiter.api.Test
1010

11-
class BankServiceTest {
11+
class BankServiceTest : BankTest() {
1212
private val id = BankAccountId.random()
1313
private val idFactory = { id }
14-
private val bank = BankService(InMemoryBankAccountRepository(), idFactory)
14+
override val bank = BankService(InMemoryBankAccountRepository(), idFactory)
1515

1616
@Test
1717
fun createsAnAccount() {
1818
val account = bank.createAccount()
1919

2020
assertThat(account, equalTo(BankAccount(id, Amount.ZERO)))
2121
}
22-
23-
@Test
24-
fun listsAccounts() {
25-
val account1 = bank.createAccount()
26-
val account2 = bank.createAccount()
27-
28-
val accounts = bank.listAccounts()
29-
30-
assertThat(accounts, equalTo(listOf(account1, account2)))
31-
}
3222
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package lmirabal.bank
2+
3+
import com.natpryce.hamkrest.assertion.assertThat
4+
import com.natpryce.hamkrest.equalTo
5+
import org.junit.jupiter.api.Test
6+
7+
abstract class BankTest {
8+
abstract val bank: Bank
9+
10+
@Test
11+
fun listsAccounts() {
12+
val account1 = bank.createAccount()
13+
val account2 = bank.createAccount()
14+
15+
val accounts = bank.listAccounts()
16+
17+
assertThat(accounts, equalTo(listOf(account1, account2)))
18+
}
19+
}

domain/src/test/kotlin/lmirabal/bank/data/InMemoryBankAccountRepository.kt renamed to domain/src/testFixtures/kotlin/lmirabal/bank/data/InMemoryBankAccountRepository.kt

File renamed without changes.

0 commit comments

Comments
 (0)