Skip to content

Commit

Permalink
Introduces initMocksBeforeTest (fixes #55)
Browse files Browse the repository at this point in the history
  • Loading branch information
SalomonBrys committed May 6, 2023
1 parent 7f21a66 commit 2af48b3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,21 @@ class MyTests : TestsWithMocks() { //<1>

NOTE: Properties delegated to `withMocks` will be (re)initialized *before each tests*, after the mocks have been (re)injected.

[CAUTION]
====
Because of https://youtrack.jetbrains.com/issue/KT-54932[this issue], you cannot consider that the mocks have been initialized in yout `@BeforeTest` methods.
You can override `initMocksBeforeTest` if you need to initialize your mocks before each test:
[source,kotlin]
----
class MyTests : TestsWithMocks() {
override fun initMocksBeforeTest() {
// Access all injected values:
// mocks, fakes & withMocks properties
}
}
----
====

== Setup

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ public abstract class TestsWithMocks {
protected fun <T : Any> withMocks(create: () -> T): Deferred<T> = Deferred(create)

@BeforeTest
public fun setUpMocksBeforeTest() {
public fun injectMocksBeforeTest() {
mocker.reset()
setUpMocks()
initDeferred()
initMocksBeforeTest()
}

public open fun initMocksBeforeTest() {}

protected abstract fun setUpMocks()

public fun <T> every(block: ArgConstraintsBuilder.() -> T) : Mocker.Every<T> =
Expand Down

0 comments on commit 2af48b3

Please sign in to comment.