0

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 = "";
}};
Community
  • 1
  • 1
Arya
  • 2,809
  • 5
  • 34
  • 56

1 Answers1

1

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.

Rogério
  • 16,171
  • 2
  • 50
  • 63
  • You are right in this specific use case. But in a complex private method that depends on some classes (method calls), i think it's not so bad to mock it instead of mocking all of dependent classes. – Arya Oct 29 '14 at 20:44
  • @Rogério what is possible way in 1.48 jmockit? – spandey Aug 31 '20 at 06:51