Integration tests for your Solr config

Solr is a search server that bundles a lot of useful Lucene modules and provides an HTTP interface for querying and updating the data. The index and most of the query mechanisms are configured using XML documents, client applications normally don’t need to be changed when adjusting the server configuration. As the server configuration heavily influences the quality of your users search experience it’s a good idea to implement some integration tests that validate your functionality.
Solr ships with a useful abstract JUnit test case that can be used as a basis for your integration tests. I will demonstrate how to fire up a simple test using maven.
The most important part is the dependencies section of the pom:

        <!-- dependencies needed for Solr integration test-->

We are including a recent JUnit version to enjoy some of the annotation goodness. solr-core contains all of the server components as well as the test case, slf4j is used for logging. Of course you have to check if any of the artifacts conflict with runtime or compile time dependencies.
To run a simple test case against the example index config shipped with solr 1.4.1 copy or link the folder apache-solr-1.4.1/example/solr/ to your projects basedir.
An example test case that checks if a value is found for a valid search:

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;
    public String getSchemaFile() {
        return "solr/conf/schema.xml";
    public String getSolrConfigFile() {
        return "solr/conf/solrconfig.xml";
    public void setUp() throws Exception {
        server = new EmbeddedSolrServer(h.getCoreContainer(), h.getCore().getName());
    public void testThatNoResultsAreReturned() throws SolrServerException {
        SolrParams params = new SolrQuery("text that is not found");
        QueryResponse response = server.query(params);
        assertEquals(0L, response.getResults().getNumFound());
    public void testThatDocumentIsFound() throws SolrServerException, IOException {
        SolrInputDocument document = new SolrInputDocument();
        document.addField("id", "1");
        document.addField("name", "my name");
        SolrParams params = new SolrQuery("name");
        QueryResponse response = server.query(params);
        assertEquals(1L, response.getResults().getNumFound());
        assertEquals("1", response.getResults().get(0).get("id"));


  1. You probably don't need the servlet dependency for this as well.

  2. Thanks for the hint, that's a good point.

  3. btw, nice test

  4. The unit test leaves a thread open.
    best it to also create an
    public void destroy() {