GC_
GC_

Reputation: 508

How to spy on a method in Java with Junit?

I like Jasmine, because you can check if a function was called, and what was passed to it.

Is there a way do this with junit and java?

Basically, I have function that calls a log function. I want to assure that function is being call, and is being called with the correct values.

Note, we only have one production build, there is not a test build and production, so mocking make be an issue.

GC_

Upvotes: 0

Views: 694

Answers (1)

abedurftig
abedurftig

Reputation: 1248

You want to look into Mockito. You can use it in your unit tests to mock a service and then also spy on it. Below is a Kotlin pseudo code.

@Mock
private val myService : MyService? = null

@InjectMocks
private val myController: MyController? = null

myController?.controllerStuff(param1)
Mockito.verify(myService, Mockito.times(1))?.doSomething(param2)

Upvotes: 1

Related Questions