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!
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.
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");
}
}
}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
--