我正在使用 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
需要进行以下扩展:
-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" );
// ...
}
}
评论:
这就是为什么例如无法使用 TestNG - TestNG 没有 RunWith 注释。
关于elasticsearch - 从 ESIntegTestCase 抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43522911/