Unit test verbosity

Here is a sample test method I found in one of my customer’s codebase. What do you think? What’s the minimum number of lines really needed to write the exact same test?

The sad thing is that, in the codebase, there are hundreds of tests like this one. All copy/pasted from the same verbose template.

/**
 * 
 */
@Test
public void testGetCustomerOK() {

  LOGGER.info("======= testGetCustomerOK starting...");

  try {
    CustomerDTO targetDTO = this.serviceImpl.getCustomer("ABC99");

    // Vérifier.
    Assert.assertNotNull("Extra non trouvé.", targetDTO);
    Assert.assertEquals("Les accountId doivent être identiques.",
	    "ABC99", targetDTO.getAccountId());

  } catch (CustomerBusinessException exception) {
    LOGGER.error("CustomerBusinessException : {}",
	    exception.getCause());
    Assert.fail(exception.getMessage());
  } catch (UnavailableResourceException exception) {
    LOGGER.error("UnavailableResourceException : {}",
	    exception.getMessage());
    Assert.fail(exception.getMessage());
  } catch (UnexpectedException exception) {
    LOGGER.error("UnexpectedException : {}" +
	    exception.getMessage());
    Assert.fail(exception.getMessage());
  } catch (Exception exception) {
    LOGGER.error("CRASH : " + exception.getMessage());
    Assert.fail(exception.getMessage());
  }

  LOGGER.info("======= testGetCustomerOK done.");
}

9 thoughts on “Unit test verbosity”

  1. @Test
    public void testGetCustomerOK() {
    Assert.assertEquals(“Les accountId doivent être identiques.”,
    “ABC99”, this.serviceImpl.getCustomer(“ABC99”).getAccountId());
    }

  2. Imho, the saddest thing in this code, is that the log does not contain any relevant information. It just create noise :
    – the first line give no information since maven already produce a per-method summary
    – the exception logs only print the message and not the stacktrace, which is already printed by the fail method
    – the last line does not tell that the test is successful even if it’s the only possible case

  3. I end up with

    @Test
    public void can_obtain_a_customer_by_account_id() throws Exception {
    assertEquals(“ABC99”, serviceImpl.getCustomer(“ABC99”).getAccountId());
    }

  4. My I suggest you submit this code to TheDailyWtf.com ? This snippet sounds like a good fit for their already huge collection of WTFy code 😉

  5. Sad is the word. Though I find it quite funny 😀 (I suppose the further away you are the funnier it gets …)

    I’m wondering what kind of reasons there can be for sucha a situation. I’m not sure sheer incompetence of one developer is sufficient. Got any ideas David?

  6. Most likely because of management tracking lines of code per developer. You get what you measure, not what you want.

Comments are closed.