Merl's Blog

AAA

I have been learning more about TDD and how to implement it. This has been helpful because in general, learning is challenging but specifically creating tests for abstract concepts such as Servers is tough especially when you are new to it.

Luckily I have access to a great resource in the the Clean coders videos. Now I just want to focus on the simple concept of Arrange, Act and Assert.

Arrange

Here you Arrange everything you need for the test. Only arrange elements directly relevant to the test to avoid unnecessary complexity!

This might mean using builders or factory patterns to handle the more complex setups without making a mess. Keeping it manageable and scalable is the name of the game here.

Keep it Light: Stick to the essentials for setup. Make it Reusable: Lean on helper methods or fixtures for common setups to stay efficient and avoid repetition. Focus on Clarity: Ensure the setup is straightforward, no need for unnecessary complexity .

Act

This is where the action happens, pun intended. This runs the actual code that you’re testing. It should be a simple and directly call method or function.

Test one behavior or aspect at a time. This not only keeps tests clean but also ensures that each test remains focused on a single responsibility.

Assert

Now we Assert whether the action has led to the expected result. Assertions should only test for outcomes that are direct results of the act phase. It is important to only test one logical concept at a time. It is possible to have multiple physical assertions in one test.

Best Practices for Assert: Specificity: Be as specific as possible in your assertions to avoid false positives. Failures as Guides: Write your assertions in a way that, when they fail, they provide clear guidance on what went wrong.

Example

This is code I took from the Advanced TDD example. Uncle Bob created a name inverter program that can take someone’s name like “Merl Martin” and return “Martin, Merl”

That sounds very simple, and it can be but it was very well tested, as well as taking into account multiple edge cases to be a robust program.

public class NameInverterTest {
  private NameInverter nameInverter; //Arrange

  @Before
  public void setup() { //Arrange
    nameInverter = new NameInverter();
  }

  private void assertInverted(String originalName, String invertedName) { //Action
    assertEquals(invertedName, nameInverter.invertName(originalName));
  }

 @Test
  public void ignoreHonorifics() throws Exception { //Assert
    assertInverted("Mr. First Last", "Last, First");
    assertInverted("Mrs. First Last", "Last, First");
  }
}

Getting the hang of the AAA pattern is like getting a SOLID grip on the backbone of TDD. It structures your tests in a way that makes them easy to follow and maintain. By getting better at Arrange, Act, and Assert, I can help write better tests to aid me in my production code. Now I can make changes whenever I need to, as long as my tests pass.

Until next time!

Merl