I am new to Mockito.
Given the class below, how can I use Mockito to verify that someMethod
was invoked exactly once after foo
was invoked?
public class Foo
{
public void foo(){
Bar bar = new Bar();
bar.someMethod();
}
}
I would like to make the following verification call,
verify(bar, times(1)).someMethod();
where bar
is a mocked instance of Bar
.
If you inject the Bar instance, or a factory that is used for creating the Bar instance (or one of the other 483 ways of doing this), you'd have the access necessary to do perform the test.
Factory Example:
Given a Foo class written like this:
public class Foo {
private BarFactory barFactory;
public Foo(BarFactory factory) {
this.barFactory = factory;
}
public void foo() {
Bar bar = this.barFactory.createBar();
bar.someMethod();
}
}
in your test method you can inject a BarFactory like this:
@Test
public void testDoFoo() {
Bar bar = mock(Bar.class);
BarFactory myFactory = new BarFactory() {
public Bar createBar() { return bar;}
};
Foo foo = new Foo(myFactory);
foo.foo();
verify(bar, times(1)).someMethod();
}
Bonus: This is an example of how TDD(Test Driven Development) can drive the design of your code.
The classic response is, "You don't." You test the public API of Foo
, not its internals.
Is there any behavior of the Foo
object (or, less good, some other object in the environment) that is affected by foo()
? If so, test that. And if not, what does the method do?
Foo
is public void foo()
, where the internals are only bar related.
verify()
become very helpful indeed, even if you are no longer worshipping at the holy altar of integration testing.
If you don't want to use DI or Factories. You can refactor your class in a little tricky way:
public class Foo {
private Bar bar;
public void foo(Bar bar){
this.bar = (bar != null) ? bar : new Bar();
bar.someMethod();
this.bar = null; // for simulating local scope
}
}
And your test class:
@RunWith(MockitoJUnitRunner.class)
public class FooTest {
@Mock Bar barMock;
Foo foo;
@Test
public void testFoo() {
foo = new Foo();
foo.foo(barMock);
verify(barMock, times(1)).someMethod();
}
}
Then the class that is calling your foo method will do it like this:
public class thirdClass {
public void someOtherMethod() {
Foo myFoo = new Foo();
myFoo.foo(null);
}
}
As you can see when calling the method this way, you don't need to import the Bar class in any other class that is calling your foo method which is maybe something you want.
Of course the downside is that you are allowing the caller to set the Bar Object.
Hope it helps.
I think Mockito @InjectMocks
is the way to go.
Depending on your intention you can use:
Constructor injection Property setter injection Field injection
More info in docs
Below is an example with field injection:
Classes:
public class Foo
{
private Bar bar = new Bar();
public void foo()
{
bar.someMethod();
}
}
public class Bar
{
public void someMethod()
{
//something
}
}
Test:
@RunWith(MockitoJUnitRunner.class)
public class FooTest
{
@Mock
Bar bar;
@InjectMocks
Foo foo;
@Test
public void FooTest()
{
doNothing().when( bar ).someMethod();
foo.foo();
verify(bar, times(1)).someMethod();
}
}
Solution for your example code using PowerMockito.whenNew
mockito-all 1.10.8
powermock-core 1.6.1
powermock-module-junit4 1.6.1
powermock-api-mockito 1.6.1
junit 4.12
FooTest.java
package foo;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
//Both @PrepareForTest and @RunWith are needed for `whenNew` to work
@RunWith(PowerMockRunner.class)
@PrepareForTest({ Foo.class })
public class FooTest {
// Class Under Test
Foo cut;
@Mock
Bar barMock;
@Before
public void setUp() throws Exception {
cut = new Foo();
}
@After
public void tearDown() {
cut = null;
}
@Test
public void testFoo() throws Exception {
// Setup
PowerMockito.whenNew(Bar.class).withNoArguments()
.thenReturn(this.barMock);
// Test
cut.foo();
// Validations
Mockito.verify(this.barMock, Mockito.times(1)).someMethod();
}
}
https://i.stack.imgur.com/gHdue.png
Yes, if you really want / need to do it you can use PowerMock. This should be considered a last resort. With PowerMock you can cause it to return a mock from the call to the constructor. Then do the verify on the mock. That said, csturtz's is the "right" answer.
Here is the link to Mock construction of new objects
Another simple way would be add some log statement to the bar.someMethod() and then ascertain you can see the said message when your test executed, see examples here: How to do a JUnit assert on a message in a logger
That is especially handy when your Bar.someMethod() is private
.
I had this very issue today, and I didn't want to use PowerMock or other stuff. I just wanted to make a test that made sure a certain method was called. I found this post and I saw that nobody had mentioned this approach.
One way of achieving this without adding in more dependencies or similar is pretty low tech, but it works:
@Test
public void testSomeMethodIsCalledOnce() throws Exception {
final AtomicInteger counter = new AtomicInteger(0);
Mockito.when(someObject.theMethodIWant(anyString()))
.then((Answer<ReturnValue>) __ -> {
teller.incrementAndGet();
return theExpectedAnswer;
});
theObjectUnderTest.theMethod(someTestValue);
assertEquals(1, teller.get());
}
This is pretty simple, and it's easy to see what's going on. When the method I want is called (it's mocked here), do this stuff. Amongst the stuff is a call to incrementAndGet for the AtomicInteger. You could use an int[] here, but that's not as clear in my opinion. We're just using something that's final, which we can increment. That's a limitation of the lambda we're using.
It's a bit crude, but it gets the job done in a simple and straightforward matter. At least if you know your lambdas and Mockito.
Success story sharing
Bar bar = mock(Bar.class)
instead ofBar bar = new Bar();
BarFactory myFactory = mock(BarFactory.class); when(myFactory.createBar()).thenReturn(bar);