class Adder {
fun add(op1: Int, op2: Int) {
return op1 + op2
}
}
class BusinessLogicService(
private val persistenceService: MyPersistenceService
) {
fun doSomethingRelevant(objectId: Long, someValue: Int) {
val myObject = persistenceService.retrieveObject(objectId)
?: throw IllegalArgumentException("$objectId not found")
myObject.someProperty = someValue
persistenceService.saveObject(myObject)
}
}
mockk.io
class BusinessLogicServiceTest {
@MockK
private lateinit var persistenceService : MyPersistenceService
private lateinit var service : BusinessLogicService
@Before
fun setup() {
MockKAnnotations.init(this)
service = BusinessLogicService(persistenceService)
}
@Test
fun `doing something with a non-existing object throws an exception`() {
every {
persistenceService.retrieveObject(any())
} returns null
assertFailsWith<IllegalArgumentException> {
service.doSomethingRelevant(123L, 345)
}
}
}
@Test
fun `doing something with an existing object updates it and saves it`() {
every {
persistenceService.retrieveObject(any())
} returns MyClass()
service.doSomethingRelevant(123L, 345)
verify {
persistenceService.saveObject(any())
}
}
@Test
fun `doing something with an existing object updates it and saves it`() {
every {
persistenceService.retrieveObject(any())
} returns MyClass()
service.doSomethingRelevant(123L, 345)
verify {
persistenceService.saveObject(match {
it.someProperty == 345
})
}
}
phake.readthedocs.io
docs.python.org/3/library/unittest.mock.html