TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

JUnit

Beginners Guide for writing Good Unit Tests

JUnit Tutorial

Overview

In this post, beginners Guide for writing Good Unit Tests, we will discuss some guidelines for writing good JUnit tests.

Naming convention:

Name your tests properly as per the naming convention (often mentioned in the organization Coding standard documents). Test case class names should reflect the class they are intended to test. Also, this avoids confusion and makes your tests and test classes evident of functionality that they intended to test. Furthermore, test case methods should reflect the scenario they are intended to test.

Example

Example shown below:

public class CalculatorTests { 
  @Test 
  public void addIntegerTest()
  {
   ....
   }
 
  @Test 
  public void multiplyIntegerTest()
  {
    ...
   }
 
  ..
  ..
  ....
}
Full Automated & CI runs

Unit tests should be fully automated. Good unit tests are tests that require no manual intervention. A continuous integration Jenkins unit tests build needs to be run whenever application code changes.

Do not hard code

Do not hard code test data in your test cases. ( dates , config filenames , number formats, etc )

Small and Independent

Tests should be independent of each other. Avoid coupling between tests. Check a single thing at a time.Don not complicate the tests. In addition, coupled testcases make debugging difficult and somewhat shield changes to them.

Do not assume

Do not assume the order in which tests run. By default, JUnit do not run tests in a predictable way. Furthermore, the order may change from JVM to JVM.

Use proper asserts

Use proper asserts that give verbose information when things go wrong as shown in below pictures.

Guidelines_For_GoodUnitTests1

Prefer above than below one.

Guidelines_For_GoodUnitTests2

 

JUnit Tutorial: https://www.testingdocs.com/junit-tutorial/

More information on JUnit can be found on the official website: https://junit.org

Related Posts

Getting Started with JUnit Framework

JUnit /

Getting Started with JUnit Framework

Run JUnit tests from command line

JUnit /

Run JUnit tests from command line

Working with JUnit in Eclipse IDE

JUnit /

Working with JUnit in Eclipse IDE

Adding JUnit5 library to a Project

JUnit /

Adding JUnit5 library to a Project

Test Failure JUnit

JUnit /

Debug JUnit Tests using IDE

‹ Unit testing Questions› JUnit Annotations order

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version