I have a very simple Check class that has a waitForCondition() blocking method. This method blocks. I want to create some unit tests for this method. First, the method should return when the condition is met. Secondly, the method should return when it is interrupted.
Internally, the Check class has an ArrayBlockingQueue and calls its take() method, so my test really deals with the correct encoding of the logic for the condition (as it should be). In the application, data for the Check class is supplied by another stream through the InputData method. The InputData method executes the logic of the input data and places the dummy object in the ArrayBlockingQueue when the condition is met. This should return waitForCondition() .
So, my first thought: I can check the InputData , taunting and checking that the dummy object is added to the queue when the condition is met. This would require a redesign of the class, since the queue is a private member of the data (unless it is possible to deride private data). Instead of InputData , adding directly to the queue when the condition is met, it would have to call something that could be taunted.
But then the problem arises of checking the waitForCondition() methods themselves, provided that InputData working correctly. This is really just code:
try { myArrayBlockingQueue.take(); return true; } catch (InterruptedException ex) { return false; }
So, I am wondering if this problem should be imagined: a test that creates another thread with Check calls waitForCondition() , and then returns something when it is done. Perhaps using the Contractorโs service. The fuzzy part is the assertTrue(...) synchronization assertTrue(...) . I found this article on asynchronous testing , which looks like it could do the trick.
Question Summary:
- Should I change the design to check the logic in
InputData() , and if so, how? - Should I leave the
waitForCondition() test as long as InputData() is being tested? - Or is it better to just do what needs to be done (somewhat complicated unit test) and check
waitForCondition() directly?
source share