小编kar*_*hik的帖子

JUnit测试AspectJ

我正在尝试为Custom Aspect编写Junit测试.这是Aspect Class Snippet:

@Aspect
@Component
public class SampleAspect {

    private static Logger log = LoggerFactory.getLogger(SampleAspect.class);

    @Around("execution(* org.springframework.data.mongodb.core.MongoOperations.*(..)) || execution(* org.springframework.web.client.RestOperations.*(..))")
    public Object intercept(final ProceedingJoinPoint point) throws Throwable {
        logger.info("invoked Cutom aspect");
         return point.proceed();

    }

}
Run Code Online (Sandbox Code Playgroud)

因此,只要关节点与切入点匹配,上述方面就会截获.它的工作正常.

但我的问题是如何对该类进行单元测试.我有以下Junit测试:

@Test(expected = MongoTimeoutException.class)
    public void TestWithMongoTemplate() {
        //MongoDocument class
        TestDocument test = new TestDocument();

        ApplicationContext ctx = new AnnotationConfigApplicationContext(TestMongoConfigurationMain.class);
        MongoTemplate mongoTemplate = ctx.getBean(MongoTemplate.class);

        //this call is being intercepted by SampleAspect
        mongoTemplate.save(test);

    }
Run Code Online (Sandbox Code Playgroud)

因此,我mongoTemplate.save(test)在Junit中被拦截,SampleAspect因为它匹配切入点.但是,我应该如何确保junits(可能通过声明)SampleAspect在调用该关节点时我正在拦截?

我不能断言返回值,intercept()因为除了执行关节点之外没有什么特别之处.因此,我的Junit无法找到任何区别,无论是由方面执行还是基于返回值的常规执行. …

java junit aspectj spring-aop mockito

17
推荐指数
1
解决办法
2万
查看次数

Jacoco离线仪器Gradle脚本

我试图寻找Jacoco离线仪器gradle脚本片段,但找不到.是否可以通过gradle脚本进行Jacoco离线检测?如果是的话...它的一个例子就是伟大的.谢谢.

java gradle jacoco build.gradle

8
推荐指数
3
解决办法
4329
查看次数

AspectJ 的集成测试

我正在尝试为自定义方面编写集成测试。这是方面类代码段。

@Aspect
@Component
public class SampleAspect {

    private static Logger log = LoggerFactory.getLogger(SampleAspect.class);

   private int count;

   public int getCount(){
      return count;
   }

    public void setCount(){
      this.count= count;
    }


    @Around("execution(* org.springframework.data.mongodb.core.MongoOperations.*(..)) || execution(* org.springframework.web.client.RestOperations.*(..))")
    public Object intercept(final ProceedingJoinPoint point) throws Throwable {
        logger.info("invoked Cutom aspect");
         setCount(1);
         return point.proceed();

    }

}
Run Code Online (Sandbox Code Playgroud)

因此,只要关节点与切入点匹配,上述方面就会拦截。它的工作正常。但我的问题是如何执行集成测试。

我所做的是在 Aspect 中创建了用于跟踪的属性“count”,并在我的 Junit 中对其进行了断言。我不确定这是否很好,或者是否有更好的方法对方面进行集成测试。

这是我所做的 Junit 的片段。我以糟糕的方式呈现,但我希望它对我为集成测试所做的工作是不可理解的。

@Test
public void testSamepleAspect(){
   sampleAspect.intercept(mockJointPoint);
   Assert.assertEquals(simpleAspect.getCount(),1);
}
Run Code Online (Sandbox Code Playgroud)

java junit integration-testing aspectj mockito

6
推荐指数
1
解决办法
4587
查看次数

注释不起作用的Spring重试框架

我使用SimpleRetryPolicy/RetryTemplate尝试使用Simple java类的spring retry框架.它工作正常.所以我想用Annotations做同样的事情.注释从来没有对我有用.在网上找不到太多帮助.下面的代码就像普通的Java程序一样,在第一次尝试时抛出异常,这不是预期的行为.在抛出异常或从中恢复之前,它应该至少试过5次.不知道我哪里出错了.我是否需要执行任何XML/spring AOP配置才能工作?怎么会这样

import org.springframework.context.annotation.Configuration;
import org.springframework.retry.annotation.EnableRetry;

@EnableRetry
@Configuration
public class App {  
    public static void main(String[] args) throws Exception {
        Parentservice p = new  SpringRetryWithHystrixService();
        String status = p.getStatus();
        System.out.println(status);
    }
}

import org.springframework.retry.annotation.Retryable;
public interface Parentservice {
    @Retryable(maxAttempts=5)
    String getStatus() throws Exception;
}


import org.springframework.retry.annotation.Recover;
public class SpringRetryWithHystrixService implements Parentservice{

    public static int i=0;

    public String getStatus() throws Exception{
        System.out.println("Attempt:"+i++);
        throw new NullPointerException();
    }

        @Recover
        public static String getRecoveryStatus(){
            return "Recovered from NullPointer Exception";
        }
    }
Run Code Online (Sandbox Code Playgroud)

我在界面和实现的顶部尝试了@Retryable(maxAttempts …

java spring-retry

5
推荐指数
1
解决办法
3469
查看次数

Jhipster-配置多个数据源

我有springBatch App,我在其中使用多个数据源,如下所示进行配置,这非常简单。工作正常。

如何在Spring中使用2个或更多数据库?

现在,我不得不将其集成到Jhipster项目中。我在application.yml中做了类似的配置。我从application.yml中删除了自动生成的数据源,并添加了与上述内容类似的配置,并将主数据源注入了Jhipster生成的类DataBaseConfiguration.java中。使用此配置,我无法对JHipster UI上的数据库实体执行CRUD操作。我没有在日志中看到任何错误。

我不确定在Jhipster项目中配置多个数据源的正确/简单方法是什么。使用多个数据库的示例示例将为我提供一个良好的开端。我没有找到太多资源。

下面是我为在JHipster中具有多个数据源而执行的更改的代码示例

# ===================================================================
# Spring Boot configuration for the "dev" profile.
#
# This configuration overrides the application.yml file.
#
# More information on profiles: http://www.jhipster.tech/profiles/
# More information on configuration properties: http://www.jhipster.tech/common-application-properties/
# ===================================================================

# ===================================================================
# Standard Spring Boot properties.
# Full reference is available at:
# http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
# ===================================================================

spring:
    profiles:
        active: dev
        include: swagger
    devtools:
        restart:
            enabled: true
        livereload:
            enabled: false # we use gulp + BrowserSync for livereload
    jackson: …
Run Code Online (Sandbox Code Playgroud)

spring-data-jpa jhipster

5
推荐指数
1
解决办法
4000
查看次数