4

I have an object

object Foo {
    fun doSomething(param: String) {
        throw Exception()
    }
}

I want it to become a stub (relaxed mock in mockk terminology) in my test.

Other words, I want this test to pass without exception:

@Test 
fun shouldAskFooWithCorrectParams() { 
    mockkObject(Foo) // How to change it to make Foo a stub
    Foo.doSomething("hey!")
    verify(exactly = 1) { Foo.doSomething("hey!") }
}
vdshb
  • 1,949
  • 2
  • 28
  • 40

1 Answers1

2

Additional every { Foo.doSomething(any()) } answers {} does the trick for a single method.

This test is passes:

@Test
fun shouldAskFooWithCorrectParams() {
    mockkObject(Foo) 
    every { Foo.doSomething(any()) } answers {}
    Foo.doSomething("hey!")
    verify(exactly = 1) { Foo.doSomething("hey!") }
}
vdshb
  • 1,949
  • 2
  • 28
  • 40