Solr searching with SimpleQuery(string) can be a powerful search tool for users. Lucene searches like (name:memory AND name:corsair) AND popularity:[6 TO *] allow users to retrieve the exact results they’re looking for. The downside is that it’s easy for the user to enter a malformed query string resulting in a Solr Exception.
Here’s the new search page you’ll find in NixMash Spring online.
If the user enters a search string with a bad field name, or say, two colons between the name and the search term, we want a clean message displayed on the page.
We’re going to go about testing for that, but before we do let’s look at the Solr Repository method which will throw the exception. As you see we’re testing for an UncategorizedSolrException.
Let’s start in the Solr project where we’ll test the Service Implementation before moving to the Mvc project where we’ll test the Controller.
We’re going to enter a few malformed queries followed by a good one. Our total number of exceptions caught should equal our number of bad queries.
Now onto the Mvc project where we test the controller. Before viewing the test let’s look at the controller where a simple UserQuery object is passed from the search form to /products/list as a Model Attribute (1). If the Service returns an uncategorizedSolrException we’ll flag a rejection on the query field of the UserQuery object and send an error back to the Search Page (2).
Now for the test. It’s important to note that we’re mocking the MVC Controller, but NOT the Product Service or Repository otherwise no exception will be thrown (1). We then enter a bad Solr query string with name1:memory, as there is no name1 field. We check for the error in the query field of the UserQuery Model Attribute and that the product.search.error message is passed back to the Search Page for the user to try again.