Java Testing - Fundamentals


1. Testing code


We're always willing to start coding 😋, but first, we need to know how to apply a minimum of theory!


Use descriptive naming for methods.

Test the behaviour rather than the implementation. Let's think about a class like a private property: we want to test the behaviour of classes, the properties they exhibit, but NOT the implementation detail of them.

Follow DRY principle < don't repeat yourself! >: e.g. make use  of constants, avoid duplication, etc.

Make use of the following annotations, in order to optimize our code and reduce duplication:

  • @BeforeEach
  • @AfterEach
  • @BeforeAll
  • @AfterAll

Make use of Junit and Hamcrest features, e.g,:

It is strongly recommended to write failure messages in order to display a reason on screen when a test failure happens. In this way, we improve the readability of our tests and it is easier to debug errors when testing our functionalities, either by implementing them from scratch or by modifying them. 


2. Writing good tests


3. TDD

Rules:

  1. Write tests before the implementation❌
  2. Implement in order to pass tests✅
  3. Refactor. Tidy and clean your code up.
  4. Repeat

¡Crea tu página web gratis! Esta página web fue creada con Webnode. Crea tu propia web gratis hoy mismo! Comenzar