I want to test process method of the ProcessServiceImpl
class, with mocking private methods: readContent
and isValidFile
and 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);
return updatedUser;
} else {
throw new InvalidFileException();
}
}
private String readContent(File inputFile) {
//implementation
}
private boolean isValidFile(String fileContent) {
//implementation
}
}
I mocked up UserService
and injected it successfully. but i can't mock the private methods of the class under the test. Based on this and this links i tried to mock them with NonStrictExpectations
but it has not any invoke
method!! Is there another way to do this? (I'm using jmockit-1.8)
new NonStrictExpectations(processServiceImpl) {{
invoke(processServiceImpl, "readContent");
result = "";
}};
The invoke(...)
methods are from the mockit.Deencapsulation
class.
But I would recommend to not mock the private
methods. Instead, use a real file with valid contents for your tests.