testing convention - jamongx/twitter-clone GitHub Wiki

Source Code Structure

  • It's a good idea to keep the test classes separate from the main source code. So, they are developed, executed, and maintained separately from the production code.
  • Also, it avoids any possibility of running test code in the production environment.
  • We can follow the steps of the build tools like Maven and Gradle that look for src/test directory for test implementations.

Package Naming Convention

  • We should create a similar package structure in the src/test directory for test classes.
  • Thus, improving the readability and maintainability of the test code.

Test file Naming Convention

  • In unit testing, add the postfix 'Tests' to the main source code file name.
  • In integration testing, add the postfix 'IT' to the main source code file name, particularly for controllers.

Unit Test Case Naming Convention

  • The test names should be insightful, and users should understand the behavior and expectation of the test by just glancing name itself.
  • Given/when/then BDD style
givenUserObject_whenSaveUser_theReturnSavedUser
givenUsersList_whenFinAll_theReturnListOfUsers
givenUserObject_whenUpdateUser_thenReturnUpdatedUser

Appropriate Assertions

  • Always use proper assertions to verify the expected vs. actual results.
  • We should use various methods available in the Assert class of JUnit or similar frameworks like AssertJ.