16

I'm coding some tests for my solr-indexer application. Following testing best practices, I want to write code self-dependant, just loading the schema.xml and solrconfig.xml and creating a temporary data tree for the indexing-searching tests. As the application is most written in java, I'm dealing with SolrJ library, but I'm getting problems (well, I'm lost in the universe of corecontainers-coredescriptor-coreconfig-solrcore ...) Anyone can place here some code to create an Embedded Server that loads the config and also writes to a parameter-pased data-dir?

javanna
  • 59,145
  • 14
  • 144
  • 125
Lici
  • 998
  • 4
  • 13
  • 22
  • 3
    BE CAREFUL!!!! Only JUnit 4.7 supports the approaches outlined below, I believe. There is some kind of setup() dynamic invocation error that occurs in newer versions of JUnit with the Solr Base test. – jayunit100 Feb 02 '12 at 17:21
  • I had a go at answering a similar question here: http://stackoverflow.com/a/23102896/1410035 – Tom Saleeba Apr 16 '14 at 07:37

3 Answers3

13

You can start with the SolrExampleTests which extends SolrExampleTestBase which extends AbstractSolrTestCase .

Also this SampleTest.

Also take a look at this and this threads.

Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
  • 1
    SolrExampleTests: http://svn.apache.org/repos/asf/lucene/solr/tags/release-1.4.0/src/test/org/apache/solr/client/solrj/SolrExampleTests.java – disco crazy Aug 26 '11 at 15:32
  • 2
    This refers to the 1.4 versions of solr. If you need the 3.5 versions of those classes there is a solr-test-framework artifact on maven central. http://mvnrepository.com/artifact/org.apache.solr/solr-test-framework – unscene Feb 26 '12 at 17:21
8

This is an example for a simple test case. solr is the directory that contains your solr configuration files:

import java.io.IOException;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.util.AbstractSolrTestCase;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.SolrParams;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class SolrSearchConfigTest extends AbstractSolrTestCase {

    private SolrServer server;

    @Override
    public String getSchemaFile() {
        return "solr/conf/schema.xml";
    }

    @Override
    public String getSolrConfigFile() {
        return "solr/conf/solrconfig.xml";
    }

    @Before
    @Override
    public void setUp() throws Exception {
        super.setUp();
        server = new EmbeddedSolrServer(h.getCoreContainer(), h.getCore().getName());
    }

    @Test
    public void testThatNoResultsAreReturned() throws SolrServerException {
        SolrParams params = new SolrQuery("text that is not found");
        QueryResponse response = server.query(params);
        assertEquals(0L, response.getResults().getNumFound());
    }

    @Test
    public void testThatDocumentIsFound() throws SolrServerException, IOException {
        SolrInputDocument document = new SolrInputDocument();
        document.addField("id", "1");
        document.addField("name", "my name");

        server.add(document);
        server.commit();

        SolrParams params = new SolrQuery("name");
        QueryResponse response = server.query(params);
        assertEquals(1L, response.getResults().getNumFound());
        assertEquals("1", response.getResults().get(0).get("id"));
    }
}

See this blogpost for more info:Solr Integration Tests

fhopf
  • 301
  • 2
  • 5
  • 2
    I would caveat all answers with a new answer : which is - if you are using junit 4.7, you can use this methodology. If your using a higher JUnit, for now, you cannot. – jayunit100 Feb 02 '12 at 17:19
5

First you need to set your Solr Home Directory which contains solr.xml and conf folder containing solrconfig.xml, schema.xml etc.

After that you can use this simple and basic code for Solrj.

File solrHome = new File("Your/Solr/Home/Dir/");
File configFile = new File(solrHome, "solr.xml");
CoreContainer coreContainer = new CoreContainer(solrHome.toString(), configFile);
SolrServer solrServer = new EmbeddedSolrServer(coreContainer, "Your-Core-Name-in-solr.xml");
SolrQuery query = new SolrQuery("Your Solr Query");
QueryResponse rsp = solrServer.query(query);
SolrDocumentList docs = rsp.getResults();
Iterator<SolrDocument> i = docs.iterator();
while (i.hasNext()) {
      System.out.println(i.next().toString());
}

I hope this helps.

Mawia
  • 4,220
  • 13
  • 40
  • 55