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:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.7</version> <type>jar</type> <scope>test</scope> </dependency> <!-- dependencies needed for Solr integration test--> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-core</artifactId> <version>1.4.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.6.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>test</scope> </dependency>
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 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")); } }
Ravish Bhagdev (@RavBhagdev)
You probably don't need the servlet dependency for this as well.
Florian Hopf
Thanks for the hint, that's a good point.
Arno Broekhof
btw, nice test
Arno Broekhof
The unit test leaves a thread open.
best it to also create an
@After
public void destroy() {
h.getCoreContainer().shutdown();
}