Skip to main content

TinT: Avoiding User Interactions

This episode of Testing in the Trenches describes, with appropriate modifications to protect the parties involved, a situation I encountered on a client's project that challenged our efforts to create a suite of automated unit tests and what we did about it. It is adapted from one of the internal Tips that I regularly sent to the team.

One of the key goals in unit testing is that we can run the suites of tests quickly. When the tests run in just a handful of seconds, we get fast feedback when issues arise. It is easier and safer to resolve those issues when the developer's mind is still on the problem they were working on.

And tests that run quickly are less of a perceived interruption to the work of a test-averse developer. This increases the odds that the developer will adopt the practice of running the test suite before committing their code.

But a run of tests comes to a complete halt when user interaction is required. For example, a path through the code that creates a pop-up message box brings the test run to a halt. None of the tests can proceed until they receive user input.

How can we write our unit tests to be independent of user interaction? It is tough when we want to test code that looks like this:

if ( myFile.getPath().length() > uiFields.getField(downLoadFileFN).getMaxWidth() )
{
   JOptionPane.showMessageDialog(getFrameInFocus(),
      "The selected path exceeds the width of the entry field.\n"
      + "Please select a different path."
      "Error",
      JOptionPane.ERROR_MESSAGE);
   doSomethingHere();
}

It may be an important condition in our logic, one that really needs to be covered by one or more unit tests. But as soon as a test fulfills the if-condition, a warning box pops up and everything pauses until it is dismissed. That is, of course, the behavior we want in production, but it is not helpful in automated testing.


There are at least a couple things we can do when faced with code that halts for a pop-up warning message or error. Both involve making a small change to the production code, to make it possible to get the tests in place. But in both cases, the small change improves our design in a small way, and both are also reasonably safe.

Option 1 – Extract Method Locally and Override in Test
If the behavior of alerting the user with a pop-up is a truly exceptional situation, one that happens very rarely in the code, it might be enough to handle it locally. In this case, extract the JOptionPane call out to a new method on the class, such as:
protected void showErrorMessage(String message, String title)
{
   JOptionPane.showMessageDialog(getFrameInFocus(),
      message, title,
      JOptionPane.ERROR_MESSAGE);
}

And then change the original code to call this method (Tip: use the refactoring tools built into your IDE to do this extraction faster and more safely).
if ( myFile.getPath().length() > uiFields.getField(downLoadFileFN).getMaxWidth() )
{
   showErrorMessage("The selected path exceeds the width of the entry field.\n"
      + "Please select a different path.", "Error");
   doSomethingHere();
}

Then the test class uses a private inner class that overrides the protected message and skips the user-interaction.
@Override
protected void showErrorMessage(String message, String title)
{
   // skip UI in Unit Tests
}

This approach is not to everyone's taste. It has some design flaws, which we can dig into another day. But it does let us move ahead with writing unit tests that are not interrupted by user interactions, and I think it makes a tiny improvement in our design.


Option 2 – Wrap the Call in Another Class
In the original production code, our class knows and uses the gory details of how to make a pop-up message appear, in this case using JOptionPane. But if we pull that code out into another class, our validation check will no longer need to know if we are using a GUI, a web interface, a shell interface, or some futuristic 3D tactile gizmo.

Then, if we switch from Java Swing to JavaFX or any other UI system for our error messages, this piece of code will not be affected, only the new class we are about to create.

Let's start it something like this:
public class UIMessagesManager
{
   public void showErrorMessage(String message, String title)
   {
      JOptionPane.showMessageDialog(getFrameInFocus(),
         message, title,
         JOptionPane.ERROR_MESSAGE);
   }
}

UIMessagesManager now has the showErrorMessage() method, for starters. It can grow over time to have utility methods that handle most different types of messates we would want to communicate to the user: an error, message or information.

Even better, the UIMessagesManager can be expanded to know whether to show a GUI, display a console string, craft a web-page response, etc. It is a much more appropriate place than our original code to handle the different UI modalities.

We can also expand it to build in some special processing for when unit tests are running. "Unit-Test UI" is conceptually another UI variation like "GUI" or "Console" except that it would suppress the UI display so that no message box appears.

In fact, let's turn it into an interface, and implement the interface with our production GUI code on the one hand, and our unit-test non-UI code on the other hand. Something like this:
public interface UIMessagesManagerIF
{
   public void showErrorMessage(String message, String title);
}

Then our production code becomes:
public class UIMessagesManager implements UIMessagesManagerIF
{
   @Override
   public void showErrorMessage(String message, String title)
   {
      JOptionPane.showMessageDialog(getFrameInFocus(),
         message, title,
         JOptionPane.ERROR_MESSAGE);
   }
}

And we can make a Unit Test UI Manager something like this:
public class FakeUIMessagesManager implements UIMessagesManagerIF
{
   private List<String> messages = new ArrayList<String>();
   @Override
   public void showErrorMessage(String message, String title)
   {
      messages.add(message);
   }
   
   public void clearMessages()
   {
      messages.clear();
   {
   
   public List<String> getMessages()
   {
      return messages;
   }
}

Our Fake class handles calls to show the user a message, and keeps the messages it was asked to display, which our tests can access later. And it provides a way to clear the log. These are outside the interface, and will be used by our unit tests for verification. Production code will know nothing about them.

Now let's write a jUnit test case for our validation check above. In a new jUnit class, we can add the following:

public class MyClassTestUT
{
   FakeUIMessagesManager myUIMgr = new FakeUIMessagesManager();
   
   @Before
   public void setup()
   {
      injectMessageManager(myUIMgr);
   }

   @After
   public void teardown()
   {
      FakeUIMessagesManager.clearMessages();
   }

   @Test
   public void validationCheck_FilePathTooLong_ExpectedMessage()
   {
      My Class objToTest = new MyClass();  // create an object of the class to test.
      objToTest.isValidInput(); // call the validation check method
      String expectedErr = "The selected path exceeds the width of the entry field.\n"
         + "Please select a different path."
      assertEquals(expectedErr, myUIMgr.getMessages().get(0));
   }
}

Even as I write this, I can think of improvements to the design of this new unit testing infrastructure, and the design of our production code.

But even this small change has real advantages. The code is now testable in an automated test suite; the production code has also become shorter, clearer, and at a higher level of abstraction, all of which make it easier to read, write and maintain; and it wraps a 3rd-party API so that, if ever we needed to switch from JOptionPane to something else, the change would happen in 1 class, not hundreds.

Popular posts from this blog

How to do Git Rebase in Eclipse

This is an abbreviated version of a fuller post about Git Rebase in Eclipse. See the longer one here : One side-effect of merging Git branches is that it leaves a Merge commit. This can create a history view something like: The clutter of parallel lines shows the life spans of those local branches, and extra commits (nine in the above screen-shot, marked by the green arrows icon). Check out this extreme-case history:  http://agentdero.cachefly.net/unethicalblogger.com/images/branch_madness.jpeg Merge Commits show all the gory details of how the code base evolved. For some teams, that’s what they want or need, all the time. Others may find it unnecessarily long and cluttered. They prefer the history to tell the bigger story, and not dwell on tiny details like every trivial Merge-commit. Git Rebase offers us 2 benefits over Git Merge: First, Rebase allows us to clean up a set of local commits before pushing them to the shared, central repository. For this

Java 8: Rewrite For-loops using Stream API

Java 8 Tip: Anytime you write a Java For-loop, ask yourself if you can rewrite it with the Streams API. Now that I have moved to Java 8 in my work and home development, whenever I want to use a For-loop, I write it and then see if I can rewrite it using the Stream API. For example: I have an object called myThing, some Collection-like data structure which contains an arbitrary number of Fields. Something has happened, and I want to set all of the fields to some common state, in my case "Hidden"

Git Reset in Eclipse

Using Git and the Eclipse IDE, you have a series of commits in your branch history, but need to back up to an earlier version. The Git Reset feature is a powerful tool with just a whiff of danger, and is accessible with just a couple clicks in Eclipse. In Eclipse, switch to the History view. In my example it shows a series of 3 changes, 3 separate committed versions of the Person file. After commit 6d5ef3e, the HEAD (shown), Index, and Working Directory all have the same version, Person 3.0.

Code Coverage in C#.NET Unit Tests - Setting up OpenCover

The purpose of this post is to be a brain-dump for how we set up and used OpenCover and ReportGenerator command-line tools for code coverage analysis and reporting in our projects. The documentation made some assumptions that took some digging to fully understand, so to save my (and maybe others') time and effort in the future, here are my notes. Our project, which I will call CEP for short, includes a handful of sub-projects within the same solution. They are a mix of Web APIs, ASP MVC applications and Class libraries. For Unit Tests, we chose to write them using the MSTest framework, along with the Moq mocking framework. As the various sub-projects evolved, we needed to know more about the coverage of our automated tests. What classes, methods and instructions had tests exercising them, and what ones did not? Code Coverage tools are conveniently built-in for Visual Studio 2017 Enterprise Edition, but not for our Professional Edition installations. Much less for any Commun

Scala Collections: A Group of groupBy() Examples

Scala provides a rich Collections API. Let's look at the useful groupBy() function. What does groupBy() do? It takes a collection, assesses each item in that collection against a discriminator function, and returns a Map data structure. Each key in the returned map is a distinct result of the discriminator function, and the key's corresponding value is another collection which contains all elements of the original one that evaluate the same way against the discriminator function. So, for example, here is a collection of Strings: val sports = Seq ("baseball", "ice hockey", "football", "basketball", "110m hurdles", "field hockey") Running it through the Scala interpreter produces this output showing our value's definition: sports: Seq[String] = List(baseball, ice hockey, football, basketball, 110m hurdles, field hockey) We can group those sports names by, say, their first letter. To do so, we need a disc