简体   繁体   English

Elasticsearch 集成测试和 spring-test 不能很好地协同工作 - AccessControlException

[英]Elasticsearch Integration test and spring-test doesn't work well together - AccessControlException

Looks like we can not have Junit test run with multiple runner.看起来我们不能用多个运行器运行 Junit 测试。 In my case, I need both RandomizedRunner (to be able to use/extend ESIntegTestCases) and SpringJUnit4ClassRunner to autowire classes under test.就我而言,我需要 RandomizedRunner(为了能够使用/扩展 ESIntegTestCases)和 SpringJUnit4ClassRunner 来自动装配被测类。

As we can specify single @RunWith annotation.因为我们可以指定单个 @RunWith 注释。 I kept @RunWith(RandomizedRunner.class).我保留了@RunWith(RandomizedRunner.class)。 To activate spring junit test support added following to my test case.激活 spring junit 测试支持添加到我的测试用例中。

@ClassRule
    public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();

@Rule
    public final SpringMethodRule springMethodRule = new SpringMethodRule();

However, when I try to run the unit test with the above setup I am facing AccessControlException.但是,当我尝试使用上述设置运行单元测试时,我遇到了 AccessControlException。

Caused by: java.security.AccessControlException: access denied ("java.lang.reflect.ReflectPermission" "suppressAccessChecks")
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
    at java.security.AccessController.checkPermission(AccessController.java:884)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
    at java.lang.reflect.AccessibleObject.setAccessible(AccessibleObject.java:128)
    at org.springframework.util.ReflectionUtils.makeAccessible(ReflectionUtils.java:207)
    at org.springframework.util.ReflectionUtils.accessibleConstructor(ReflectionUtils.java:191)
    at org.springframework.core.io.support.SpringFactoriesLoader.instantiateFactory(SpringFactoriesLoader.java:164)
    ... 17 more

Complete source of test case for reference:完整的测试用例来源供参考:

import com.carrotsearch.randomizedtesting.RandomizedRunner;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.rules.SpringClassRule;
import org.springframework.test.context.junit4.rules.SpringMethodRule;


@RunWith(RandomizedRunner.class)
@ThreadLeakScope(ThreadLeakScope.Scope.NONE)
@SpringBootTest(classes = ProductSearchDemoApplication.class)
public class ProductSearchServiceTest extends ESIntegTestCase {

    @Autowired
    private ProductService productService;

    @ClassRule
    public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();

    @Rule
    public final SpringMethodRule springMethodRule = new SpringMethodRule();

    @Test
    public void test() throws Exception {
        System.out.println("SubTestWithRunner test()");
    }
}

Would be great if someone can guide on how to ensure spring-test and ESIntegTestCase works together seamlessly.如果有人可以指导如何确保 spring-test 和 ESIntegTestCase 无缝协同工作,那就太好了。

I know it's a little bit late but we just faced many issues with ESIntegTestCase and wanted to share our solution.我知道现在有点晚了,但我们刚刚在ESIntegTestCase遇到了很多问题,想分享我们的解决方案。 We end up using Elasticsearch container from testcontainers.org as an embedded Elasticsearch for our integration tests.我们最终使用来自testcontainers.org 的Elasticsearch 容器作为我们集成测试的嵌入式 Elasticsearch。

The advantage is that you could use any version of Elasticsearch and it's pretty easy to integrate into any project.优点是您可以使用任何版本的 Elasticsearch,并且很容易集成到任何项目中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM