如何站起来然后关闭单元测试中的Solr实例?

时间:2014-06-19 13:19:33

标签: java unit-testing solr jetty apache-camel

我正在写骆驼单元测试。我与之交互的一项服务是Solr。在使用模拟测试后,我想编写测试来调出本地solr实例。

到目前为止,我已尝试利用RestTestBase Solr类(org.apache.solr.util.RestTestBase)。因为我正在编写单元测试,所以我没有扩展类,只是尝试使用它提供的静态方法。以下工作:

RestTestBase.createJettyAndHarness("src/test/resources/solr",
                                   "solrconfig.xml", "schema.xml",
                                   "/solr", true, null);
int solrPort = RestTestBase.jetty.getLocalPort();
String solrURL = "localhost:"+Integer.toString(solrPort)+"/solr";
...

我把solr和码头包括在我的pom中。关于如何编程并关闭solr实例的任何建议? (因为我正在使用骆驼单元测试,我不想扩展RestTestBase)

1 个答案:

答案 0 :(得分:1)

请参阅camel-solr单元测试以获得一些完整的示例.... SolrComponentTestSupport

基本上这个......

@BeforeClass
public static void beforeClass() throws Exception {
    // Set appropriate paths for Solr to use.
    System.setProperty("solr.solr.home", "src/test/resources/solr");
    System.setProperty("solr.data.dir", "target/test-classes/solr/data");

    // Instruct Solr to keep the index in memory, for faster testing.
    System.setProperty("solr.directoryFactory", "solr.RAMDirectoryFactory");

    // Start a Solr instance.
    solrRunner = new JettySolrRunner("src/test/resources/solr", "/solr", getPort());
    solrRunner.start();

    solrServer = new HttpSolrServer("http://localhost:" + getPort() + "/solr");
}

@AfterClass
public static void afterClass() throws Exception {
    if (solrRunner != null) {
        solrRunner.stop();
    }
}