DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Test-Driven Development With The oclif Testing Library: Part One
  • Solid Testing Strategies for Salesforce Releases
  • Practical Use of Weak Symbols
  • Apex Testing: Tips for Writing Robust Salesforce Test Methods

Trending

  • How to Convert XLS to XLSX in Java
  • Testing SingleStore's MCP Server
  • AI's Dilemma: When to Retrain and When to Unlearn?
  • How To Replicate Oracle Data to BigQuery With Google Cloud Datastream
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Test Smells: Cleaning up Unit Tests

Test Smells: Cleaning up Unit Tests

We'll write a simple unit test, show the mistakes people often make with them, and rewrite them according to best practices.

By 
Natalia Poliakova user avatar
Natalia Poliakova
·
Mikhail Lankin user avatar
Mikhail Lankin
·
Jul. 08, 24 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
3.9K Views

Join the DZone community and get the full member experience.

Join For Free

In practical terms, knowing how not to write code might be as important as knowing how to write it. This goes for test code, too; and today, we're going to look at common mistakes that happen when writing unit tests.

Although writing unit tests is common practice for programmers, tests are still often treated as second-class code. Writing good tests isn't easy — just as in any programming field, there are patterns and anti-patterns. There are some really helpful chapters on test smells in Gerard Meszaros's book about xUnit patterns — and more great stuff around the internet; however, it's always helpful to have practical examples.

Here, we're going to write one unit test quick and dirty, and then improve it like we would in our work. The full example is available on GitHub.

One Test's Evolution

To begin with, what are we testing? A primitive function:

Java
 
public String hello(String name) {
	return "Hello " + name + "!";
}


We begin writing a unit test for it:

Java
 
@Test
void test() {

}


And just like that, our code already smells.

1. Uninformative Name

Naturally, it's much simpler to just write test, test1, test2, than to write an informative name. Also, it's shorter!

But having code that is easy to write is much less important than having code that is easy to read - we spend much more time reading it, and bad readability wastes a lot of time. A name should communicate intent; it should tell us what is being tested.

A test communicating its intent

So maybe we could name the test testHello, since it's testing the hello function? Nope, because we're not testing a method, we're testing behavior. So a good name would be shouldReturnHelloPhrase:

Java
 
@Test
void shouldReturnHelloPhrase() {
	assert(hello("John")).matches("Hello John!");
}


Nobody (apart from the framework) is going to call the test method directly, so it's not a problem if the name seems too long. It should be a descriptive and meaningful phrase (DAMP).

2. No arrange-act-assert

The name is okay, but now there is too much code stuffed into one line. It's a good idea to separate the preparation, the behavior we're testing, and the assertion about that behavior (arrange-act-assert).

Arrange, act, assert

Arrange, act, assert

Like this:

Java
 
@Test
void shouldReturnHelloPhrase() {
    String a = "John";

    String b = hello("John");

    assert(b).matches("Hello John!");
}


In BDD, it's customary to use the Given-When-Then pattern, and in this case, it's the same thing.

3. Bad Variable Names and No Variable Re-Usage

But it still looks like it's been written in a hurry. What's "a"? What's "b"? You can sort of infer that, but imagine that this is just one test among several dozen others that have failed in a test run (perfectly possible in a test suite of several thousand tests). That's a lot of inferring you have to do when sorting test results!

So — we need proper variable names.

Something else we've done in a hurry — all our strings are hard-coded. It's okay to hard-code some stuff — only as long as it's not related to other hard-coded stuff!

Meaning, that when you're reading your test, the relationships between data should be obvious. Is "John" in 'a' the same as "John" in the assertion? This is not a question we should be wasting time on when reading or fixing the test.

So we rewrite the test like this:

Java
 
@Test
void shouldReturnHelloPhrase() {
    String name = "John";

    String result = hello(name);
    String expectedResult = "Hello " + name + "!";

    assert(result).contains(expectedResult);
}


4. The Pesticide Effect

Here's another thing to think about: automated tests are nice because you can repeat them at very little cost — but that also means their effectiveness falls over time because you're just testing the exact same thing over and over. That's called the pesticide paradox (a term coined by Boris Beizer back in the 1980s): bugs build resistance to the thing you're killing them with.

It's probably not possible to overcome the pesticide paradox completely — but there are tools that reduce its effect by introducing more variability into our tests, for instance, Java Faker. Let's use it to create a random name:

Java
 
@Test
void shouldReturnHelloPhrase() {
    Faker faker = new Faker();
    String name = faker.name().firstName();

    String result = hello(name);
    String expectedResult = "Hello " + name + "!";

    assert(result).contains(expectedResult);
}


Good thing we've changed the name to a variable in the previous step — now we don't have to look over the test and fish out all the "Johns."

5. Uninformative Error Messages

Another thing we've probably not thought about if we've written the test in a hurry — is the error message. You need as much data as possible when sorting test results, and the error message is the most important source of information. However, the default one is pretty uninformative:

java.lang.AssertionError at org.example.UnitTests.shouldReturnHelloPhrase(UnitTests.java:58)

Great. Literally the only thing this we know is that the assertion hasn't passed. Thankfully, we can use assertions from JUnit's `Assertions` class. Here's how:

Java
 
@Test
void shouldReturnHelloPhrase4() {
    Faker faker = new Faker();
    String name = faker.name().firstName();

    String result = hello(name);
    String expectedResult = "Hello " + name + "";

    Assertions.assertEquals(
        result,
        expectedResult
    );
}


And here's the new error message:

Expected :Hello Tanja! Actual :Hello Tanja

...which immediately tells us what went wrong: we've forgotten the exclamation mark!

Lessons Learned

And with that, we've got ourselves a good unit test. What lessons can we glean from the process?

A lot of the problems were caused by us being a bit lazy. Not the good kind of lazy, where you think hard about how to do less work. The bad kind, where you follow the path of least resistance, to just "get it over with."

Hard-coding test data, doing cut and paste, using "test" + method name (or "test1", "test2", "test3") as the name of the test are marginally easier to do in the short run, but make the test base much harder to maintain.

On the one hand, it is a bit ironic that we've been talking about readability and making tests easier on the eyes, and at the same time turned a 1-line test into 9 lines. However, as the number of tests you're running grows, the practices we're proposing here will save you a lot of time and effort.

Error message Test data unit test

Published at DZone with permission of Natalia Poliakova. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Test-Driven Development With The oclif Testing Library: Part One
  • Solid Testing Strategies for Salesforce Releases
  • Practical Use of Weak Symbols
  • Apex Testing: Tips for Writing Robust Salesforce Test Methods

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!