Sunday 15 June 2014

java - Mock private methods of the class under test in jmockit -



java - Mock private methods of the class under test in jmockit -

i want test process method of processserviceimpl class, mocking private methods: readcontent , isvalidfile , userservice class.

@named("processserviceimpl") public class processserviceimpl { @inject private userservice userservice; public user process(long userid, file inputfile) throws invalidfileexception { user user = userservice.load(userid); string filecontent = readcontent(inputfile); if (isvalidfile(filecontent)) { user updateduser = userservice.updateuser(user, filecontent); homecoming updateduser; } else { throw new invalidfileexception(); } } private string readcontent(file inputfile) { //implementation } private boolean isvalidfile(string filecontent) { //implementation } }

i mocked userservice , injected successfully. can't mock private methods of class under test. based on this , this links tried mock them nonstrictexpectations has not invoke method!! there way this? (i'm using jmockit-1.8)

new nonstrictexpectations(processserviceimpl) {{ invoke(processserviceimpl, "readcontent"); result = ""; }};

the invoke(...) methods mockit.deencapsulation class.

but recommend not mock private methods. instead, utilize real file valid contents tests.

java testing junit mocking jmockit

No comments:

Post a Comment