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

JUnit

Getting Started with JUnit Framework

JUnit Tutorial

Introduction

Getting Started with JUnit Framework: JUnit is a simple framework to write repeatable tests. It is a unit testing framework for Java, created by Erich Gamma and Kent Beck. It is based on the xUnit architecture for unit testing frameworks. In this post, we will write a sample JUnit test and see how to run it.

Prerequisites

  • JDK.
  • IDE like Eclipse.

Let’s get started!

The easiest way to install JUnit is to add the JUnit library to the project build path.

or

Add the maven dependency and add it to the Maven project pom.xml file.

https://www.testingdocs.com/junit-unit-testing-framework/

Sample test

import static org.junit.Assert.*;
import org.junit.Test;

public class FirstTest {

@Test
public void testConcat() {
String s1 = "abc";
String s2 = "xyz";
assertEquals("abcxyz", s1 + s2);
}
}

 

JUnit_First_Test

 

The simplest way to run the test is Right-click on the class >> Run As >> JUnit Test  as shown in the below picture:

 

Run_JUnit_Test

 

BlockJUnit4ClassRunner

It is the default test runner that runs the tests and reports the test results for us under the hood. If we do not want the default runner to be used, we can specify the runner by annotating our test class with the @RunWith annotation. We talk about this annotation later.

Learn more: JUnit Calculator Test cases 

 

Related Posts

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

Adding Calculator Test Methods in Eclipse

JUnit /

Adding Calculator Test Methods in Eclipse

‹ Run JUnit tests from command line

Recent Posts

  • Update draw.io on Windows
  • 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