0

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)

revivn
  • 73
  • 7
  • Note that using two different mocking APIs in the same test doesn't really make sense, since each API has its own ways for everything. In JMockit, for example, you would use the `any`, `anyInt`, `anyString`, etc. fields for argument matching. Another thing is that `any(Car.class)`, `anyString()`, etc. (and their equivalents in JMockit) are argument matchers to be used when *recording* or *verifying* expectations, and not anywhere else. – Rogério Jan 14 '15 at 14:11
  • I first try to use all jmockit but failed,I must have done something wrong.I'll update question show more detail code – revivn Jan 15 '15 at 02:19
  • You are making several mistakes in the use of the mocking API. I would suggest to spend a few minutes with the [documentation](http://jmockit.org/gettingStarted.html). – Rogério Jan 15 '15 at 14:20

1 Answers1

0

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");
shikjohari
  • 2,278
  • 11
  • 23