elasticsearch - 从 ESIntegTestCase 抛出 NullPointerException

标签 elasticsearch nullpointerexception integration-testing

我正在使用 Elasticsearch 2.4.4 版。
然后我创建了从 ESIntegTestCase 派生的测试用例,例如:

public class ELSTest extends ESIntegTestCase {

    @Override
    protected Settings nodeSettings ( int nodeOrdinal ) {
        return Settings.builder().put( super.nodeSettings( nodeOrdinal ) )
            .put( super.nodeSettings( nodeOrdinal ) )
            .put( IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1 )
            .put( IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1 )
            .put( Node.HTTP_ENABLED, true )
            .build();
    }

    @BeforeClass
    public void setup () throws Exception {
        createIndex( "idx" ); // line 57
        ensureGreen( "idx" );
    }
}

在解决了“jar hell”问题并阅读了一堆关于 ESIntegTestCase 的页面后,我得到了新的。
java.lang.NullPointerException
at org.elasticsearch.test.ESIntegTestCase.client(ESIntegTestCase.java:657)
at org.elasticsearch.test.ESIntegTestCase.client(ESIntegTestCase.java:650)
at org.elasticsearch.test.ESIntegTestCase.prepareCreate(ESIntegTestCase.java:763)
at com.company.ELSTest.setup(ELSTest.java:57)

可能是什么原因?我的意思是 ESIntegTestCase 类在行中抛出 NPE 的那个:
Client client = cluster().client();

似乎集群未初始化。在测试中启动 elasticsearch 的正确基本类设置是什么?

最佳答案

回答自己:-)

文档位于:integration tests

需要进行以下扩展:

  • 重要的限制是 ESIntegTestCase 类只能与 JUnit 一起使用。没有简单的方法可以使它与例如TestNG - 解释如下。
  • 使用附加参数启动 JUnit:

    -Dtests.jarhell.check=false
    避免 jar hell

    -Dtests.security.manager=false
    避免 java.security.AccessControlException: access denied
  • 类声明如下:

    @RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class)
    公共(public)类 ELSTest 扩展 ESIntegTestCase {
    @Before
    public void setup () throws Exception {
    
        beforeClass(); // against NullPointerException in ESIntegTestCase
    
        createIndex( "idx" );
        ensureGreen( "idx" );
    
        // ...
    }
    

    }

  • 评论:
  • 需要@RunWith 以避免 java.lang.IllegalStateException: No context information for thread:
    这就是为什么例如无法使用 TestNG - TestNG 没有 RunWith 注释。
  • @Before 不能更改为 @BeforeClass,因为不能从静态上下文调用 beforeClass() 方法。
  • 方法 nodeSettings ( int nodeOrdinal ) 不是强制性的。
  • 关于elasticsearch - 从 ESIntegTestCase 抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43522911/

    相关文章:

    标记化字段上的 ElasticSearch 术语聚合

    elasticsearch - 将 Sonarqube 6.7 连接到外部 Elasticsearch

    Java:空指针异常

    testing - 使用 .NET Core 和 NUnit 进行集成测试

    php - 拉拉维尔 : Integration tests failing on bitbucket pipelines

    java - ElasticSearch TransportClient 版本 5.6

    elasticsearch - 如何在 ElasticSearch 中进行嵌套聚合,其中聚合超过子聚合的结果

    java - 如何从 Android 的内部存储中获取 .mp3 文件

    c# - 在 Visual Studio 中针对多个配置运行单个测试