Integration testing with Solr made easy
See Contributing
<dependency>
<groupId>no.finn.search</groupId>
<artifactId>solr-integrationtest-support</artifactId>
<version>7.0.0</version>
<scope>test</scope>
</dependency>
repositories {
jcenter()
}
testCompile "no.finn.search:solr-integrationtest-support:73.0.0
- For now, you'll need to run singlethreaded, because the EmbeddedSolrServer locks the indexdirectory (see pom.xml for configuration of surefire.)
- We usually put our Solr Configs in src/main/resources or src/test/resources
- Have your JUnit test extend
SolrTestCase
- Test your schema and solrconfig.xml
Example setup is available for both maven and gradle inside the exampleprojects folder
import no.finn.solr.integration.SolrTestCase;
public class SolrConfigTest extends SolrTestCase {
@Test
public void weCanFindADocumentWeJustAdded() throws Exception {
Long docId = solr.addDocumentWith("Hello");
solr.performSearchAndAssertHits("hello", docId);
}
}
For other examples - see the provided SolrExampleTestCase
Our goal is that the Javadocs should be all you need.
But a quickstart is available below:
Long docId = solr.addDocumentWith(content);
Adds a single document to the index with content
to what is set as defaultContentField
.
By default this is the body
field.
Returns the randomly generated id of the document. The id can be used later for verification
Long[] docIds = solr.addDocumentsWith(... content);
A varargs version of addDocumentWith
, this adds content to the defaultContentField
Returns an array of the documentIds acquired when adding the content
Long docIds = solr.addDocumentWithField(field, content);
Adds a single document to the index with content
to the field in field
Returns the randomly generated id of the document
Long[] docIds = solr.addDocumentsWithField(field, ... content);
A varargs version of addDocumentWithField
. This adds contents to the field
Returns an array of the documentIds acquired when adding the content
QueryResponse response = solr.search(String query);
QueryResponse response = solr.dismaxSearch(String dismaxQuery);
Uses the dismax queryhandler instead of the standard/default handler (qt=dismax). This performs a search and updates the response field on the class. Call
solr.search(field, query);
Offers a simple way to run a query against a specific field
If you've set parameters directly on SolrClient or with solr.withParam(..)
you can execute a search by calling
QueryResponse response = solr.search();
This will then perform a search with the parameters currently set.
solr.performSearchAndAssertOneHit(String search);
Calls search, and then verifies that the result has exactly one hit
solr.performSearchAndAssertHits(String search, Long... ids);
Calls search, and then verifies that the hits returned have exactly the ids passed in. Works well with the ids returned from the
solr.addDocument_
methods
solr.performSearchAndAssertNoHits(String search);
Typically you'd want to confirm that a certain word does not yield results
import no.finn.solr.integration.SolrTestCase;
public class NonExistantDocumentTest extends SolrTestCase {
@Test
public void documentDoesNotExist() throws Exception {
Long docId = solr.addDocumentWith("doc");
solr.performSearchAndAssertNoHits("doctor");
}
}
Makes sure that the index does not contain anything that matches the passed in search