简体   繁体   中英

what is the correct way to make mocks when testing a method?

I am new using mocks , specially jMock (for java). I've done some testing before but not with mock and I am wondering what is the correct way to make them.

For example, I have a service with the implementation.. so, I have this service test. At the server test I have my methods.

Lets say I have methodA on serviceHelperTest , and as part of methodA I have this

ArrayList someValues = serviceHelperImpl.otherMethod

So, as far as I know, I should mock this serviceHelperImpl.othermethod , because I don't really care if is good or not, it is supposed to return some values.

So, let's say I expect a boolean value, is it possible to do this?

public myMethodToTest(){
     mockery.checking(new Expectations() {
     {
                oneOf(serviceHelper).otherMethod();
                will(returnValue(true));


            }
        });


     Boolean myVar = serviceHelper.otherMethod();
     ...
}

I dont know if this is correct or not... any idea how mocks should be implemented on testing units?

I'm not really sure about why are you trying to save the return value from serviceHelper.otherMethod(); if the purpouse of this is to test otherMethod you should only call serviceHelper.otherMethod(); without save the returned value in a variable. Remember that mock tests are to check the "state" of a function, not the result. If you want to check the result you should use xUnit tests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM