JUnit测试模拟JoinPoint

340 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。 ​今天的积累都是为了更好的明天,加油!我是java程序员可以关注我一起学习哈!

JUnit测试模拟JoinPoint:

/** * Pointcut that matches all Spring beans in the application's main packages. */ @Pointcut("within(com.cybersite.repository..*)"+ " || within(com.cybersite.service..*)"+ " || within(com.cybersite.web.rest..*)") public void applicationPackagePointcut() { // Method is empty as this is just a Pointcut, the implementations are in the advices. } /** * Retrieves the {@link Logger} associated to the given {@link JoinPoint}. * * @param joinPoint join point we want the logger for. * @return {@link Logger} associated to the given {@link JoinPoint}. */ private Logger logger(JoinPoint joinPoint) { return LoggerFactory.getLogger(joinPoint.getSignature().getDeclaringTypeName()); }

答案:

JoinPoint joinPoint = mock(JoinPoint.class); MethodSignature signature = mock(MethodSignature.class); String typeName = "some name"; when(joinPoint.getSignature()).thenReturn(signature); when(joinPoint.getSignature().getDeclaringTypeName()).thenReturn(typeName);