英文:
Need to mock a JoinPoint for JUnit test
问题
我需要模拟一个用于 JUnit 测试的 Spring Java JoinPoint。请参见下面的代码。
/**
* 匹配应用程序主包中的所有 Spring bean 的切入点。
*/
@Pointcut("within(com.cybersite.repository..*)" +
" || within(com.cybersite.service..*)" +
" || within(com.cybersite.web.rest..*)")
public void applicationPackagePointcut() {
// 由于这只是一个切入点,方法为空,实现在通知中。
}
/**
* 检索与给定 {@link JoinPoint} 相关联的 {@link Logger}。
*
* @param joinPoint 我们想要日志记录器的连接点。
* @return 与给定 {@link JoinPoint} 相关联的 {@link Logger}。
*/
private Logger logger(JoinPoint joinPoint) {
return LoggerFactory.getLogger(joinPoint.getSignature().getDeclaringTypeName());
}
有人知道我如何创建模拟的 JoinPoint 吗?
英文:
I need to mock a Spring Java JoinPoint for a JUnit test. See code below.
/**
* 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());
}
Anyone know how I would create the mock JoinPoint?
答案1
得分: 3
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);
英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论