This past week I worked on utilizing Solr’s AbstractSolrTestCase which extends JUnit’s TestCase. In theory, this makes it easier to create tests that hit an index and run thru the entire search pipeline if necessary.
Unfortunately, there isn’t a ton of docs to help out but there are plenty of examples within Solr’s source to help.
That being said, here are a few things I found out while working with it.
Because of the way the setUp method worked, I needed to basically duplicate much of its functionality instead of calling super.setUp(). By default, the setUp method will create the data directory for Solr in java.io.tmpdir (generally /tmp on Unix systems) and then the name of the class plus a timestamp. This was a problem for us because it meant that the index would be created for each test in a new directory.
I realize the need for having atomic data for unit tests but I viewed these Solr tests more as integration tests than true unit tests. They were going thru the entire system as opposed to focusing on just one class or section.
To create the index, we were hooking pieces up to our current indexing pipeline, a very nice plug-in system we developed to go through various stations to either clean data or retrieve more of it. Thankfully only a few places actually interacted with Solr so I was able to mock that communication out and just use the data collected and give it to the adoc / update methods.
Because the pipeline wasn’t instantaneous, I wanted to reuse the indexes as much as possible. I figured a good middle ground for this would be for each test class to have its own index and all it to give the indexing pipeline information about what data it wanted to index. That index would stay until a physical directory was deleted and then it would be recreated with updated data.
So I basically had to copy much of the existing setUp method and create the data directory with the test class name but no timestamp as well as make the tearDown method a no-op.
With all of this done, I now have a class which any developer can extend which hopefully will increase our test coverage.
- BROWSE / IN TIMELINE
- « A Good Day for Hadoop
- » The Whuffie Factor
- BROWSE / IN Development Search Solr
- « Testing with Redis
COMMENTS / 2 COMMENTS
Hoss added these pithy words on Jun 29 09 at 10:51 pmThe abstract test case was really designed to be a base for *Solr* tests, or Solr plugins — hence the purity of a clean data dir before every test is run. As you say: mocking is definitely the way to go if you want to test the interaction of external systems with Solr (no need to even have Solr running, let alone running inside the actual test case, or with a particular index.)
That said: a patch to refactor a new “getDataDir()” method out of setUp so that subclasses could override should be fairly trivial if you’d like to submit one. I don’t have the code in front of me, but I think it would even be fairly easy to make it so that if a subclass overrides getDataDir() to return null, then the dataDir from the solrconfig.xml file will be used (just like in a production instance)
josh added these pithy words on Jun 29 09 at 11:10 pmHoss -
Thanks for the comment! I can totally see that I’m not necessarily using the test case in its most pure form but the good news is that it is helping our developers write more tests and that’s definitely a good thing!
I’ll take a look at the code and see if I can produce a patch that’ll work.
Thanks again!
SPEAK / ADD YOUR COMMENT
Comments are moderated.
