Question

Verifications in jmockit with withCapture capturing an object does not quite work. Any help?

@Injectable
private API _api;

@Tested
private ServiceImpl _service;

@Test
public void test(){
    new Verifications() {
        {
            VirtualUser user;
            _api.add(user = withCapture());
            Assert.assertEquals("1", user.getId());
            Assert.assertEquals("user", user.getUsername());
        }
    };
    _service.add("1","user");
}

Exception: null pointer in user.getId().

Was it helpful?

Solution

Verification blocks (new Verifications() { ... }) are supposed to come after having executed the code under test. In the example test, the verification is executed before, so there is nothing to capture into the user variable at that moment. Therefore, it remains null. Simply move the verification block to the end of the test method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top