I have one UT using jmockit and mockito
in this UT,I have one field @Tested TestService testService
testService have one method using Car as parameter : testService.doTest(Car car)
then my UT: testService.doTest(any(Car.class))
when I debug this UT I found that testService.doTest(any(Car.class))
always pass null into doTest(car)
,what if I want an instance of Car in doTest(car)
?
I tried (Car)any()
,(Car)anyObject()
,none of them help,any ideas?
updated: I Have code:
class TestService{
@AutoWired
Dependency dependency;
doTest(Car car){
dependency.doSomeThing(car);
print(car.toString());
}
}
UT code:
@Injectable Dependency mockDependency;
@Tested TestService testService;
//record
new Expectations() {{
mockDependency.doSomeThing((Car)with(any));
result=detailCar;
}};
//call verify
new Verifications(){{
codeResult=testService.doTest((Car)with(any))
//some codeResult assert
}};
this all jmockit version,I get NullPointerException at car.toString()
if I change it to:
new Verifications(){{
codeResult=testService.doTest(new Car())
//some codeResult assert
}};
or
final Car car;
new Verifications(){{
codeResult=testService.doTest(car)
//some codeResult assert
}};
I get MissingInvocation at codeResult=testService.doTest(...)
seems instance car
don't match (Car)with(any)
Mock car = mock(Car.class)
and pass this car
in doTest. Hope it ll help
Also if you are calling any method of Car, make sure you also define the behaviour.
for eg
when(car.getName(anyString())).then("bmw");