Reading Time: 2 minutes

Hello Folks,

As we discussed earlier Solr with Scala and AkkaHttp in our last blog we have used Solr as a service and tried to hit solr service with the AkkaHttp and spray routes. Here is the link for the Recap.

When we want to do testing for those methods which are creating connection with the remote solr server, have to use Embedded Solr for that.

What is Embedded Solr?

Embedded Solr same interface as Solr without requiring an HTTP connection. When we “embed” Solr into a Java an application, It provides the exact same API as you would use if you were connecting to a remote Solr instance. We can use embedded solr for the in memory testing because when we implement test cases, it should not be depend on any external resources.

How can we use Embedded Solr?

When we wish to use Embedded Solr we have to follow some basic steps for creating an interface with embedded solr.

Add Solr-Core dependency. Here is a predefined directory structure, follow them : solr |-test_embedded //name of configuration folder |-conf |-lang |-currency.xml |-elevate.xml |-managed-schema |-param-.json |-protword.txt |-solrconfig.xml |-stopwords.txt |-synonyms.txt |-data (Optional if not created than it will create automatically) |-core.properties |-solr.xml Now we are ready to start embedded solr in test set-up and will shut down it after complete testing. override def before() = { val container = new CoreContainer() container.load() server = new EmbeddedSolrServer(container, "test_embedded") } test("test embedded solr"){ val solrInputDocument = new SolrInputDocument() solrInputDocument.addField("id", 1) solrInputDocument.addField("name", "test") server.add("test_embedded",solrInputDocument) val parameter = new SolrQuery() parameter.set("q", "*:*") val result = server.query("test_embedded",parameter) val docList = result.getResults() val doc = docList.get(0) assert(doc.getFirstValue("name").toString().toLowerCase().contains("test")); } override def afterAll(): Unit = { server.close }

This is a basic code example for testing the embedded solr. If test case passes successfully, we are ready to do testing for the services.

For more example and code please refer Knoldus Github.

If You have any questions you can contact me here or on Twitter: @anuragknoldus