【问题标题】:Need to mock a JoinPoint for JUnit test需要为 JUnit 测试模拟 JoinPoint
【发布时间】:2020-08-24 19:31:42
【问题描述】:

我需要为 JUnit 测试模拟一个 Spring Java 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?

【问题讨论】:

标签: java spring testing junit


【解决方案1】:
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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多