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

JUnit

JUnit Unit Testing Framework

What is JUnit?

JUnit is a simple, open-source Unit Testing framework tool to write and run repeatable unit tests. Also, it is an instance of the xUnit architecture for unit testing frameworks. It is mostly used as a Unit testing tool by most developers.

JUnit main features are as below:

  • Annotations to mark the methods as Test methods.
  • Assertions for testing expected results/actual results.

Test runner for running tests as shown in the below picture.

 

 

Unit Testing

 

JUnit 4 Maven dependency

If your build tool is Maven, then add a dependency to JUnit to pom.xml. Replace the minor version to the latest stable version.

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.xx</version>
  <scope>test</scope>
</dependency>

 

JUnit 4 Annotations:

Annotations mark the tests as test cases ex: with @Test. The most common JUnit 4 annotations are:

@Test
@Before
@After

A simple JUnit test:

The @Test annotation tells JUnit that the method to which it is attached can be run as a test case. Also, to run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. In addition, any exceptions thrown by the test will be reported by JUnit as a failure. If no exceptions are thrown, the test is assumed to have succeeded.

 

@Test
public void testMethod()
{
assertEquals(1, 1);
}

 

https://www.testingdocs.com/anatomy-of-a-junit-test/

In conclusion, the most recommended tool for writing/executing Java unit tests is JUnit. JUnit 4 is a major API revision release  ( However, a new version JUnit5 is around the corner).

 

JUnit Tutorial on this website can be found at:

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

More information on JUnit 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

‹ Introduction to Unit Testing and its Advantages› Write JUnit Test using BlueJ IDE

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