partial mock in mockito (plus @Injectmocks)?

1,745 views
Skip to first unread message

Yang Yang

unread,
Jul 18, 2013, 10:09:46 PM7/18/13
to moc...@googlegroups.com

I have the following class:

public class Car {
    Engine engine;
    public void honk() {
       /// blahblah operations I want to mock out
    }
    public void drive() {
        honk();
        if ( engine.fire() ) {
            System.out.println("Started car");
        }
    }
}

now in my test code, I want to test the drive() method of Car, but I don't care about the details of honk(), so I want to mock out honk(); furthermore, I want to mock out engine, so I need to inject mocks for that.

here is my test:

public class CarTest {
    @InjectMocks
    Car spyCar = spy(new Car());
    @Mock
    Engine mockedEngine;
    @Test
    public void test() {
       when(mockedEngine.fire()).thenReturn(true);   /**********/
       spyCar.drive();
    }
}

the problem is that when the line above with "/**/" is called, the real impl (instead of mock) is called. where am I doing wrong?

thanks!

Marcin Zajączkowski

unread,
Jul 19, 2013, 7:35:07 PM7/19/13
to moc...@googlegroups.com
> the problem is that when the line above with "/****/" is called, the real
> impl (instead of mock) is called. where am I doing wrong?

What is called? The implementation of a method fire in a class Engine?
It would be strange.

I would expect to have a real honk() called in the line spyCar.drive()
(not the line before). To avoid this you have to stub honk method in
spyCar like:
doNothing().when(spyCar).honk();
before calling spyCar.drive().

Please remember when stubbing spies you have to use doXXX..when (or
willXXX..given) instead of when..thenXXX (given..willXXX) do prevent a
call of the real method on stubbing.

Marcin

--
http://blog.solidsoft.info/ - Working code is not enough
http://refcardz.dzone.com/refcardz/mockito - Mockito Refcard

KARR, DAVID

unread,
Jul 22, 2013, 5:00:36 PM7/22/13
to moc...@googlegroups.com

Do you have “@RunWith(MockitoJUnitRunner.class)” on the class?

 

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Yang Yang

unread,
Jul 26, 2013, 5:31:21 PM7/26/13
to moc...@googlegroups.com
I am using @RunWith(SpringJunit4Runner.class)   as I have to use Spring for some of my other classes

tam tran minh

unread,
Jan 7, 2014, 10:41:37 PM1/7/14
to moc...@googlegroups.com
the real method would be called instead of your expected mock because you forgot to put @Autowired for Engine engine
This should be:

public class Car {
    @Autowired
    Engine engine;
    public void honk() {
       /// blahblah operations I want to mock out
    }
    public void drive() {
        honk();
        if ( engine.fire() ) {
            System.out.println("Started car");
        }
    }
}

KARR, DAVID

unread,
Jan 8, 2014, 12:53:28 AM1/8/14
to moc...@googlegroups.com

To the original poster:

 

You need to have a “@Runwith(MockitoJUnitRunner.class)” annotation on the class declaration.  Otherwise your “@InjectMocks” annotation is just an annotation, with no behavior.

 

From: moc...@googlegroups.com [mailto:moc...@googlegroups.com] On Behalf Of tam tran minh


Sent: Tuesday, January 07, 2014 7:42 PM
To: moc...@googlegroups.com

--

Reply all
Reply to author
Forward
0 new messages