Knowledge Base > Troubleshooting > Working with code R004

Working with code R004

If you receive the output code R004, this means that Diffblue Cover cannot write tests for a method because it did not find a way to observe its effects in order to write meaningful assertions.

Please consider one of the following options to improve the observability of the effects of the method tested:

  • Add a package-private, protected or public getter for the field modified.
  • Change the visibility of the field modified.
  • Add a return value.

Example:

In the following snippet, the method addToCache modifies a private attribute of the class CacheUtils. The effect of addToCache cannot be observed, therefore Diffblue Cover is unable to write meaningful assertions for it and marks this method with the output code R004.

public class CacheUtils {
  private List<String> cache = new ArrayList<>();

  /**
    * Adds entry to cache
    * @param entry
    */
  public void addToCache (String entry) {
      cache.add(entry);
  }
}
​​​​​
 

The testability of addToCache can be improved by changing the visibility of the attribute cache to package-private. The effects of addToCache are now observable and Diffblue Cover successfully writes assertions for this method.

public class CacheUtils {
  List<String> cache = new ArrayList<>();
  /**
    * Adds entry to cache
    * @param entry
    */
  public void addToCache (String entry) {
      cache.add(entry);
  }
}