Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 骆驼JUnit测试:并行执行的错误结果_Spring Boot_Junit_Apache Camel_Parallel Testing - Fatal编程技术网

Spring boot 骆驼JUnit测试:并行执行的错误结果

Spring boot 骆驼JUnit测试:并行执行的错误结果,spring-boot,junit,apache-camel,parallel-testing,Spring Boot,Junit,Apache Camel,Parallel Testing,我使用camel.version 2.18.1和spring boot 1.5.1.RELEASE。 我有一些或多或少复杂的驼峰路由,其中来自MQ主题的消息将被消费、过滤、转换并最终路由到不同的MQ主题 from("{{sourceEP}}").to("{{archiveEP}}") .process(new MyProcessor()) .to("{{archiveEP}}").to("{{resultEP}}"); 应用程序属性 sourceEP=jms:topic:SOURC

我使用camel.version 2.18.1和spring boot 1.5.1.RELEASE。
我有一些或多或少复杂的驼峰路由,其中来自MQ主题的消息将被消费、过滤、转换并最终路由到不同的MQ主题

from("{{sourceEP}}").to("{{archiveEP}}")
   .process(new MyProcessor())
   .to("{{archiveEP}}").to("{{resultEP}}");
应用程序属性

sourceEP=jms:topic:SOURCE
archiveEP=jms:topic:ARCHIVE
resultEP=jms:topic:TARGET
sourceEP=direct:start
archiveEP=mock:archive
resultEP=mock:result
对于每条路线,存在超过40种不同的场景。因此,我对每个路由有将近50个JUnit测试,总共有将近400个JUnit测试,我使用maven surefire插件运行这些测试,以实现并行测试执行

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.19.1</version>
  <configuration>
    <parallel>classes</parallel>
    <threadCount>4</threadCount>
  </configuration>
</plugin>
RouteTest.java

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest(classes = MyApplication.class)
public class RouteTest {
    @Autowired
    private CamelContext context;

    @EndpointInject(uri = "{{archiveEP}}")
    protected MockEndpoint archiveEndpoint;

    @EndpointInject(uri = "{{resultEP}}")
    protected MockEndpoint resultEndpoint;

    @Produce(uri = "{{sourceEP}}")
    protected ProducerTemplate sourceEndpoint;

    @Before
    public void setup() {
        sourceEndpoint.cleanUp();
        archiveEndpoint.reset();
        resultEndpoint.reset();
    }

    @Test
    public void test1() throws Exception {
        sourceEndpoint.sendBody("some text");

        archiveEndpoint.expectedMessageCount(2);
        archiveEndpoint.assertIsSatisfied();

        resultEndpoint.expectedMessageCount(1);
        resultEndpoint.assertIsSatisfied();

        resultEndpoint.expectedBodiesReceived("expected output");
    }

    @Test
    public void test2() throws Exception {
        sourceEndpoint.sendBody("another text");

        archiveEndpoint.expectedMessageCount(2);
        archiveEndpoint.assertIsSatisfied();

        resultEndpoint.expectedMessageCount(1);
        resultEndpoint.assertIsSatisfied();

        resultEndpoint.expectedBodiesReceived("another output");
    }

    ...
} 
我的问题是,有可能并行运行骆驼路线的JUnit测试吗

我试图在测试方法上添加
@DirtiesContext
,以强制Spring测试在每个测试方法之后自动重新加载上下文:
当然,这在并行测试执行时不起作用,因为结果仍然是随机的,并且预期的消息数不正确

最后,我设置了这些测试,这些测试将测试骆驼路由到
@NotThreadSafe
,以强制执行单线程执行。只有这些JUnit测试是并行执行的,它们将测试驼峰路由以外的其他功能

但对于近400个JUnit测试的数量来说,这并不是一个令人满意的解决方案。

是否有任何配置或设置可以并行测试驼峰路由,从而正确运行?

您的MQ主题是有状态的,因此“不是线程安全的”。一旦多个测试并行运行,主题中的消息数量就不可预测了

要解决此问题,您必须隔离测试的有状态部分,即MQ主题。您必须为每个测试生成唯一的MQ主题名称,以便每个测试都有自己的MQ主题。如果是这种情况,那么消息大小的定义与单线程执行相同


或者,作为主题内隔离的替代方法,您可以使用隔离主题中的消息,以进行不同的测试。在这种情况下,每个测试必须设置一个具有唯一值的消息头,并且只使用具有该值的消息

谢谢你的回答。对于测试,我使用来自Camel的模拟端点,这会导致错误和随机的结果。也许我会尝试为每个测试设置一个单独的模拟名称。哦,对不起,我没有注意到。是的,在这种情况下,您必须确保每个测试都有自己的MockEndpoint实例