While it's relatively straightforward, it is still hampered by smells:

The method scope is polluted with unnecessary objects. The ephemeral orgs, org1, org2, and org3 variables, are only necessary for the setup of the Employee instance, which is the subject of the test.

Since object instantiation and mutation are done in independent operations, I am forced to give these objects names. Furthermore, the single method scope requires me to name all objects uniquely.

The number of similarly named variables sharing the sole scope puts me at risk of making a silly mistake. Such mistakes can be hard to spot. Did you notice the mistake in my example code?

Overall, the test reads poorly. I must wade through a dozen lines of setup code to glean the important pieces. The ratio of setup code to assertion code seems out of balance, too.

There are ways to alleviate these issues — at least in part. I could separate the setup logic into its own dedicated method, but that introduces two methods with a 1:1 coupling per test case. The collection factory methods introduced in Java 9 could benefit, but only with the creation of the Set<Org>. I could add an Org(String code) convenience constructor and perhaps a setOrgs(Org... orgs)convenience method on Employee, but this won't scale well for more complex object graphs.

Ultimately and generally speaking, I would like to consolidate instantiation and mutation concerns whenever I assign a variable reference and have these concerns isolated in their own scope.

Is there a solution?

"Tap" to the Rescue

A number of languages offer built-in methods that offer a solution to this very problem. In Kotlin, it's called "apply;" in Ruby and Groovy, it's called "tap."

Regardless of the name, the method takes as an argument with some lambda/closure/code block that can interact with the object, the method it was invoked on, and ultimately, return that object.

Here's an example of how the test method could be re-written with Groovy's tap method:

Granted, there are alternatives to tap that could be used here instead, but the point is that the tap method resolves the aforementioned smells with my original code.

It creates distinct scopes for each Org instance and the Set.

In doing so, it eliminates the requirement to give each instance a unique name.

Since the objects are not in the same scope, I avoid mistakes related to naming. There's no way for me to accidentally set the code property of the second Org instance twice, like I did in my original Java method.

Because all setup logic is contained with a sub-scope of the test method's scope, I'm better able to visually distinguish setup code from assertion code.

Mimicking Tap in Java

Although not as elegant as Groovy, a tap-like method can be created in Java fairly easily:

The Java method passes the object as an argument to the consumer, whereas the Groovy method will simply delegate to the object tap was invoked on. Because Java's Consumer needs an argument, I'm forced to name it, but at least the name is associated with its own scope.

Usage Examples

Let's look at some examples for using our new tap method. Here is the original test method re-written so that the employee assignment is tapped:

I've also discovered tap to be useful for simply isolating concerns within a unit test method. For example, consider an integration test for adding a user via a RESTful interface. There are three REST calls to make:

A GET to ensure the user does not exist before attempting to create it.

The PUT to add the user.

Another GET to make sure the user was added.

Each of these three HTTP requests can be done within its own tap call to isolate objects related to the response and its payload.