Code Smell 254 - Mystery Guest
Tags: webdev, beginners, programming, tutorial
Published: 2024-06-03 12:37:33

*You assert that something happened, but why?*

> TL;DR: Be explicit when creating tests to ensure clarity and maintainability

# Problems

- Readability

- [Coupling](https://dev.to/mcsee/coupling-the-one-and-only-software-design-problem-2pd7) to external databases, global state or [singletons](https://dev.to/mcsee/singleton-the-root-of-all-evil-50bh), [static methods](https://dev.to/mcsee/code-smell-18-static-functions-fpj) or external services

- Maintenance Difficulty

- Debugging Complexity

- Hidden Dependencies

# Solutions

1. Be Explicit

2. Inline the setup

3. Use dependency Injection

4. Use [mocking](https://dev.to/mcsee/code-smell-30-mocking-business-3glm) with caution

# Context

Your test depends on external data or configurations not immediately visible within the test itself.

This obscures the test’s setup, making it difficult for someone reading it to understand what is being tested and why it might fail. 

Every test case should have three stages:

1. Setup: Initialize and configure everything needed for the test.

2. Exercise: Execute the code being tested.

3. Assert: Verify the expected outcome.

All of them must be explicit

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/ba2c15c5d72c871a4251c9d08dfcf728)

```java
@Test
void shouldReturnAnswerWhenAnswerExists() {
    User answer = KnowledgeRepository.findAnswerToQuestion(42);
    assertNotNull(answer);
}
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/48162612d1667217eb493109f9ae8405)

```java
@Test
void shouldReturnAnswerWhenAnswerExists() {
    KnowledgeRepository knowledgeRepository = 
        new InMemoryKnowledgeRepository();
    Answer expectedAnswer = new Answer(42, "The Ultimate");
    knowledgeRepository.save(expectedAnswer);
    
    Answer actualAnswer = answerRepository.findAnswerToQuestion(42);
    assertNotNull(actualAnswer);
    assertEquals(expectedAnswer, actualAnswer);
}
```

# Detection

[X]  Manual

You can detect this smell by looking for tests that do not clearly show their setup steps or rely heavily on external configurations.

# Tags

- Test Smells

# Level

[x] Intermediate

# AI Generation

AI-generated code often avoids this smell due to the tendency to create small, isolated examples.

# AI Detection

Most AI Detectors fail to identify this as a problem unless you point it out explicitly.

# Conclusion

This code smell is especially prevalent in legacy codebases or when consistent testing practices are lacking. 

You need to be explicit of the environment since tests must always be in "full environmental control"

# Relations

{% post https://dev.to/mcsee/code-smell-17-global-functions-4b82 %}

{% post https://dev.to/mcsee/code-smell-32-singletons-1ka %}

{% post https://dev.to/mcsee/code-smell-18-static-functions-fpj %}

{% post https://dev.to/mcsee/code-smell-30-mocking-business-3glm %}

# More Info

[Craft Better Software](https://craftbettersoftware.com/p/tdd-5-test-smells-5-solutions)

{% post https://dev.to/mcsee/coupling-the-one-and-only-software-design-problem-2pd7 %}

# Disclaimer

Code Smells are my [opinion](https://dev.to/mcsee/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned-1n3a).

# Credits

Photo by [Brands&People](https://unsplash.com/@brandsandpeople) on [Unsplash](https://unsplash.com/photos/womans-face-with-green-eyes-M2cFm9iHXSc)
    
* * *

> Science is what we understand well enough to explain to a computer, Art is all the rest

_Donald Knuth_
 
{% post https://dev.to/mcsee/software-engineering-great-quotes-26ci %}

* * *

This article is part of the CodeSmell Series.

{% post https://dev.to/mcsee/how-to-find-the-stinky-parts-of-your-code-1dbc %}