Home > Blog > Unit test verbosity

Unit test verbosity

January 27th, 2012

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.");
}
Tags:
  1. January 27th, 2012 at 18:24 | #1

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

  2. January 27th, 2012 at 19:01 | #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. January 27th, 2012 at 19:03 | #3

    My bad :

    - the exception logs only print the message which is already printed by the fail method, but does not print the stacktrace.

  4. January 27th, 2012 at 19:30 | #4

    My detailed response, with intermediate steps: http://ericlefevre.net/wordpress/2012/01/27/cleaning-up-test-code/

  5. January 27th, 2012 at 19:31 | #5

    I end up with

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

  6. JB.L
    January 28th, 2012 at 10:00 | #6

    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 ;)

  7. January 29th, 2012 at 00:23 | #7

    Sad is the word. Though I find it quite funny :D (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?

  8. Skirmantas Kligys
    February 3rd, 2012 at 05:38 | #8

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

Comments are closed.