Sure, here’s the translation: 需要为JUnit测试模拟一个JoinPoint

huangapple go评论70阅读模式
英文:

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);

huangapple
  • 本文由 发表于 2020年8月25日 03:31:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63567595.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定