vendredi 16 mars 2018

Mock objects with spring aspect

I have a test class:

@RunWith(EasyMockRunner.class) 
public class TestClass{

...

@Test
public void check() {
    .. some logic ...

    expect().andReturn();
    expect().andReturn();
    replay(...);
    Map<,> result = value.collect(...);
    verify(...);
    assertTrue(...)
}

And such aspect:

@Aspect
public class SafeUriInterceptor {

@Autowired
private UriConverterUtil uriConverterUtil;

@Around("execution(public * *(..)) && @annotation(SafeUrl) && args(param)")
public void proceedMethodWithSafeUrl(ProceedingJoinPoint joinPoint, String param) throws Throwable {
    String encodedValue = uriConverterUtil.encodeUriString(param);
    joinPoint.proceed(new Object[] { encodedValue });
}

@Around("execution(public * *(..)) && @annotation(SafeUrl) && args(uriList)")
public void proceedMethodWithSafeUrlList(ProceedingJoinPoint joinPoint, List<String> uriList) throws Throwable {
    List<String> encodedUriStrList = uriList != null
            ? uriList.stream().map(uriConverterUtil::encodeUriString).collect(Collectors.toList())
            : null;
    joinPoint.proceed(new Object[] { encodedUriStrList });
}

}

In test class inside value.collect(...) this aspect performs action. When I run test I get NullPointerException, because of UriConverterUtil is null. I tried to mock UriConverterUtil, but this doesn't help. Object I mocked and object inside aspect SafeUriInterceptor are different objects. And I still get NullPointerException.

What can I do to just call method inside test without aspect? Or how can I mock UriConverterUtil inside aspect SafeUriInterceptor? Or is there another solution?

Aucun commentaire:

Enregistrer un commentaire