/solr-test-exemple

An exemple how to test solr using maven

Primary LanguageJava

Search Helper : Test Solr using Maven

Search helper is an exemple how to test Solr using Maven.

You will be able to start a new Solr Server just for your tests.

My tests extends AbstractSolrTestCase which provides some help in order to use an EmbeddedSolrServer

Because AbstractSolrTestCase is not accessible anymore with 3.1 version, I clone the file in this repository (until 4.0) : https://issues.apache.org/jira/browse/SOLR-2061

Maven : pom.xml

If you want to reproduce it at home, the whole idea is to add these dependencies in your pom.xml :

<dependency>
    <groupId>org.apache.solr</groupId>
    <artifactId>solr-core</artifactId>
    <version>1.4.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.solr</groupId>
    <artifactId>solr-cell</artifactId>
    <version>1.4.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.5.5</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.8.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>test</scope>
</dependency>

Solr config files

Now you have to copy the Solr config into $PROJECT_DIR/solr/conf

.
|-- pom.xml
|-- solr
|   |-- conf
|   |   |-- admin-extra.html
|   |   |-- elevate.xml
|   |   |-- mapping-ISOLatin1Accent.txt
|   |   |-- protwords.txt
|   |   |-- schema.xml
|   |   |-- scripts.conf
|   |   |-- solrconfig.xml
|   |   |-- spellings.txt
|   |   |-- stopwords.txt
|   |   |-- synonyms.txt
|   |   `-- xslt
|   |       |-- example_atom.xsl
|   |       |-- example_rss.xsl
|   |       |-- example.xsl
|   |       `-- luke.xsl

Junit test extends AbstractSolrTestCase

Then create a Junit test, it should extends AbstractSolrTestCase

class SearchTest extends AbstractSolrTestCase {
  EmbeddedSolrServer 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
  void testWhatYouWant() {
    ....
  }
  
}

In the setUp method, a solrServer is created.

Now enjoy !!!

The exemple

If you struggle to make it work, clone this repository and try :

mvn test

Because I love the Groovy compact syntax, I used groovy in my tests.