Here is my test:
object MySpek : Spek({
val myMock1: MyMock1 = mock()
val myMock2: MyMock2 = mock()
val handler = StartModeHandler(myMock1, myMock2)
val session = mock<Session> {
on { user }.doReturn(User.builder().withUserId("userId").build())
}
describe("item exists for user") {
beforeGroup {
reset(digitalPointDao, trackDao)
}
whenever(myMock1.loadItem(session.user.userId)).thenReturn(Optional.of(MyItem()))
whenever(myMock2.loadSomething()).thenReturn(ArrayList())
context("method onLaunch was called") {
val response = handler.onLaunch(session)
it("should return the response for existing user") {
//some asserts here
}
it("should save the item") {
//some mock verifies here
}
}
}
})
According to Spek documentation, I expect the flow to be the following:
But i'm getting the following flow:
Am I miss something and doing something wrong here?
As mentioned in the Spek documentation group scopes (given, describe, context) will eagerly execute any code within. Any test state initialization should be done within fixtures (beforeEachTest, beforeGroup, etc ...). You can also use memoized
to create a dependency tied to Spek's lifecycle.
object MySpek: Spek({
val myMock1 by memoized { mock<myMock1>() }
val myMock2 by memoized { mock<myMock2>() }
val handler by memoized { StartModeHandler(myMock1, myMock2) }
val session by memoized {
mock<Session> {
on { user }.doReturn(User.builder().withUserId("userId").build())
}
}
describe("item exists for user") {
beforeEachTest {
reset(digitalPointDao, trackDao)
whenever(myMock1.loadItem(session.user.userId)).thenReturn(Optional.of(MyItem()))
whenever(myMock2.loadSomething()).thenReturn(ArrayList())
}
// use on instead of context
on("method onLaunch was called") {
val response = handler.onLaunch(session)
it("should return the response for existing user") {
//some asserts here
}
it("should save the item") {
//some mock verifies here
}
}
}
})