🖐 5 TDD Most Common Mistakes
Tags: tutorial, programming, tdd, testing
Published: 2021-05-16 12:47:39

If you are using TDD, as you should be 🤨, you have been a victim of at least one of those anti-patterns.

We will discuss 5 🖐 of them, looking at their **Causes**, **Problems** and **how to avoid them**.  
<br />

## 1️⃣ The Liar 🤥

Passes all tests with no useful assertions

- **Causes** 
	- Chasing test coverage
- **Problem**
	- Illusiuon of coverage
	- Waste of time and money 
- **Correction**
	- Delete the tests
	
```java
    @Test
    public void liarTest(){
        ShipmentService shipmentService = new ShipmentService();
        shipmentService.doStuff();
        assertTrue(true);
    }
```  
___
## 2️⃣ Excessive Setup 🧰

 Requires a lot of work to prepare the test
- **Causes**
	- Poor modularity and poor separation of concerns in the design
- **Problem**
	- Tests and code are highly coupled
	- Hard to maintain 
	- This will slow down the development to a halt 
- **Correction**
	- Improve abstraction and separation of concerns in the code
	- Write pure functions

```java
    @Test
    public void excessiveSetup (){
        // set up beginning
        VerificationService verificationService = new VerificationService();
        ShipmentRepository shipmentRepository = new ShipmentRepository();
        ShipmentService shipmentService = new ShipmentService(verificationService, shipmentRepository);

        Product product = new Product("Product");
        Owner owner = new Owner("owner");
        Count count = new Count(100);
        Shipment shipment = new Shipment(product, owner, count);
        ShipmentDetail shipmentDetail = new ShipmentDetail(shipment);
        // set up ending

        // rest of test
    }
```
___

## 3️⃣ The Giant 💥
Has multiple assertions in a single test

- **Causes**
	- Testing too much things at the same time
- **Problem**
	- Hard to understand
	- Test and code are highly coupled
	- Hard to maintain
	
- **Correction**
	- Decompse the test into multiple tests

```java
    @Test
    public void theGiant (){
        ShipmentService shipmentService = new ShipmentService();

        Shipment shipment = new Shipment("product", "owner", 2);
        shipmentService.processShipment_Free(shipment);
        assertEquals("productUpdated", shipment.getProduct());
        assertEquals("ownerUpdated", shipment.getOwner());
        assertEquals(5, shipment.getCount());

        Shipment shipment2 = new Shipment("product2", "owner2", 9);
        shipmentService.processShipment_Premium(shipment);
        ssertEquals("productPremium", shipment2.getProduct());
        assertEquals("ownerPremium", shipment2.getOwner());
        assertEquals(41, shipment2.getCount());
        
        // could go on even further
        //....
    }
```
___

## 4️⃣ The Mockery 🤡

Uses so many mocks that the real code isn't tested
- **Causes**
	- Excessive setup

- **Problems**
	- Doesn't test anything
- **Correction**
	- Review code design
	- Improve abstraction and seperation of concerns
```java
    @Test
    public void theMockery (){
        doReturn("test").when(serviceMock).getValue();
        doReturn(true).when(serviceMock).isValid();
        
        assertEquals("test", serviceMock.getValue());
        assertTrue(serviceMock.isValid());
    }
```	

___

## 5️⃣ The inspector 🧐

Adding functions that are only used in the tests

- **Causes**
	- Chasing test coverage
	
- **Problems**
	- Waste of time and money since it doesn't test production code
- **Corrections**
	- Delete those functions.
	   
___

### The source of this information is mainly from [this](https://www.youtube.com/watch?v=UWtEVKVPBQ0&ab_channel=ContinuousDelivery) amazing YouTube video.