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

标签 java spring-boot elasticsearch junit spring-test

看起来我们无法使用多个运行程序来运行 Junit 测试。就我而言,我需要 RandomizedRunner (以便能够使用/扩展 ESIntegTestCases)和 SpringJUnit4ClassRunner 来 Autowiring 测试下的类。

因为我们可以指定单个@RunWith注释。我保留了@RunWith(RandomizedRunner.class)。要激活 spring junit 测试支持,请在我的测试用例中添加以下内容。

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

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

但是,当我尝试使用上述设置运行单元测试时,我遇到了 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

完整的测试用例来源供引用:

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()");
    }
}

如果有人可以指导如何确保 spring-test 和 ESIntegTestCase 无缝协作,那就太好了。

最佳答案

我知道现在有点晚了,但我们刚刚遇到了 ESIntegTestCase 的许多问题,并且想分享我们的解决方案。我们最终使用 Elasticsearch container来自testcontainers.org作为我们集成测试的嵌入式 Elasticsearch。

优点是您可以使用任何版本的 Elasticsearch,并且很容易集成到任何项目中。

关于java - Elasticsearch 集成测试和 spring-test 不能很好地协同工作 - AccessControlException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55952242/

相关文章:

java - 从 TableView javafx 中删除列

java - 如何在 Spring Boot 中将 Java 实体映射到多个 MongoDB 集合并为两个集合使用不同的索引?

java - 使用 Spring Boot 将 JSON 负载转换为 xml

java - MyNotificationService 需要类型为 'org.springframework.mail.javamail.JavaMailSender' 的 bean,但无法找到

c# - ElasticSearch(C#Nest)中的多个最大/最小聚合

search - 在ElasticSearch中搜索使用别名

java - 如何创建文档子集并在 Elasticsearch 中对该子集执行查询?

java - 从 Groovy 类创建 Spring bean 导致 "cannot find symbol error"

java - JXDatePicker 返回错误的日期

java - 从 DB 中检索一行作为 Hibernate 中的 Map