ElasticSearchTestCase:使用ESIntegTestCase创建客户端实例时

时间:2018-02-16 08:08:00

标签: java unit-testing elasticsearch junit integration-testing

当我声明@BeforeClass表示法并声明我的ElasticSerach客户端时 它给了我这种错误。

我希望Client实例只在我的测试类中有一个。我正在使用ESIntegTestCase此类编写测试用例。

这是我的代码。

public class PersonTest extends ESIntegTestCase {

    private static Client client;

    @BeforeClass
    public static void setUpClient() {
        client = ESIntegTestCase.client();
    }

    @Test
    public void createIndex() {
         // Using client's instance creating an index.
    }

    @Test
    public void search() {
         // Using client's instance Seach would be perform.
    }

}

我遇到了这种错误。

java.lang.NullPointerException
    at __randomizedtesting.SeedInfo.seed([F541A8F42426AB1C]:0)
    at org.elasticsearch.test.ESIntegTestCase.client(ESIntegTestCase.java:633)
    at org.elasticsearch.test.ESIntegTestCase.client(ESIntegTestCase.java:626)
    at com.saba.search.elasticSearch.PersonTest.setUpClient(PersonTest.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.carrotsearch.randomizedtesting.RandomizedRunner.invoke(RandomizedRunner.java:1713)
    at com.carrotsearch.randomizedtesting.RandomizedRunner$6.evaluate(RandomizedRunner.java:847)
    at com.carrotsearch.randomizedtesting.RandomizedRunner$7.evaluate(RandomizedRunner.java:863)
    at org.apache.lucene.util.AbstractBeforeAfterRule$1.evaluate(AbstractBeforeAfterRule.java:45)
    at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
    at org.apache.lucene.util.TestRuleStoreClassName$1.evaluate(TestRuleStoreClassName.java:41)
    at com.carrotsearch.randomizedtesting.rules.NoShadowingOrOverridesOnMethodsRule$1.evaluate(NoShadowingOrOverridesOnMethodsRule.java:40)
    at com.carrotsearch.randomizedtesting.rules.NoShadowingOrOverridesOnMethodsRule$1.evaluate(NoShadowingOrOverridesOnMethodsRule.java:40)
    at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
    at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
    at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
    at org.apache.lucene.util.TestRuleAssertionsRequired$1.evaluate(TestRuleAssertionsRequired.java:53)
    at org.apache.lucene.util.TestRuleMarkFailure$1.evaluate(TestRuleMarkFailure.java:47)
    at org.apache.lucene.util.TestRuleIgnoreAfterMaxFailures$1.evaluate(TestRuleIgnoreAfterMaxFailures.java:64)
    at org.apache.lucene.util.TestRuleIgnoreTestSuites$1.evaluate(TestRuleIgnoreTestSuites.java:54)
    at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
    at com.carrotsearch.randomizedtesting.ThreadLeakControl$StatementRunner.run(ThreadLeakControl.java:367)
    at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

我想这是因为测试集群尚未初始化而发生的。不需要使客户端变量静态。我建议使用@Before注释和实例变量