vortimeet.blogg.se

Argument captor
Argument captor







argument captor

In the example above, we used the verify () method to check the arguments passed to add (). val captor ArgumentCaptor.forClass (Callback::class) Mockito.verify (someClass).doSomeThing (captor. We can write tests for the above two methods without argument captor as well in.

Argument captor series#

This article series was written for the version 0.x.x of mockito-scala, and while the technical details and reasoning behind the new features still holds true, some parts of the API were changed from 1.x.x, so I strongly recomend to have a read to the README where the changes are explained. Argument Capture One reason to override the default behavior with doNothing () is to capture arguments. ArgumentCaptor allows us to capture an argument passed to a method to. Returns argument captor that captures values for further assertions.

see here Example: ArgumentCaptor argument ArgumentCaptor.forClass (Person.class) verify (mock).doSomething (argument.capture ()) assertEquals ('John', argument.getValue (). now, obviously, you get the same answer, regardless of the arguments.

Provide a nicer API leveraging in the Scala power.Now it’s time to move into some other features mockito-scala provides! ArgCaptor We’ve already seen how the original Mockito Java API was improved in Part 1 and also how the matchers are easier to use and more powerful in Part 2 It’s also worth to check said README to discover the new features introduced in 1.x.x #Mockito argument captor series You should use factory method forClass (Class) to create captors instead to avoid NullPointerExceptions.

argument captor

With the new API the same test would look like val aMock = mock val captor = Captor aMock.stringArgument("it worked!") verify(aMock).stringArgument( captor) captor "it worked!" I know, it’s not the end of the world, and it already looks better than the Java equivalent, but we can do even better Let’s start with the nicer API, if you have used argument captors in the past you probably noticed that they are quite verbose, requiring us to call different methods like capture() and getValue depending onwhat stage of the test we where, let’s see an example of this val aMock = mock val captor = argumentCaptor aMock.stringArgument("it worked!") verify(aMock).stringArgument(captor.capture()) captor.getValue shouldBe "it worked!"Īs you can see we have to call capture() when we pass the captor the the verify method and then getValue to check the value the captor got.









Argument captor