DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Mastering Unit Testing and Test-Driven Development in Java
  • Hints for Unit Testing With AssertJ
  • How to Migrate From JUnit 4 to JUnit 5 Step by Step
  • Testing Asynchronous Operations in Spring With JUnit 5 and Byteman

Trending

  • Artificial Intelligence, Real Consequences: Balancing Good vs Evil AI [Infographic]
  • Start Coding With Google Cloud Workstations
  • Segmentation Violation and How Rust Helps Overcome It
  • How to Build Scalable Mobile Apps With React Native: A Step-by-Step Guide
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. JUnit Tutorial for Beginners in 5 Steps

JUnit Tutorial for Beginners in 5 Steps

What is JUnit and Unit Testing? First JUnit project and Green Bar, first code and first unit test, other assert methods, and important annotations.

By 
Ranga Karanam user avatar
Ranga Karanam
DZone Core CORE ·
Jul. 27, 20 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
10.9K Views

Join the DZone community and get the full member experience.

Join For Free

master microservices

  • Git Repository — https://github.com/in28minutes/getting-started-in-5-steps/tree/master/junit-in-5-steps
  • Pre-requisites
    • Java & Eclipse — https://www.youtube.com/playlist?list=PLBBog2r6uMCSmMVTW_QmDLyASBvovyAO3
  • We will use embedded maven in Eclipse

JUnit is the most popular Java Unit testing framework

Here is an overview of what we would learn in this section

  • Step 1: What is JUnit and Unit Testing?
  • Step 2: First JUnit Project and Green Bar
  • Step 3: First Code and First Unit Test
  • Step 4: Other assert methods
  • Step 5: Important annotations

Free Courses — Learn in 10 Steps

Step 1: What Is JUnit and Unit Testing?

  • What is JUnit?
  • What is Unit Testing?
  • Advantages of Unit Testing

We typically work in large projects — some of these projects have more than 2000 source files or sometimes it might be as big as 10000 files with one million lines of code.

Before unit testing, we depend on deploying the entire app and checking if the screens look great. But that's not very efficient. And it is manual.

Unit Testing focuses on writing automated tests for individual classes and methods.

JUnit is a framework that will help you call a method and check (or assert) whether the output is as expected.

The important thing about automation testing is that these tests can be run with continuous integration — as soon as some code changes.

Step 2: First JUnit Project and Green Bar

  • What is JUnit?
  • First Project with JUnit
  • First JUnit Class
  • No Failure is Success
  • MyMaths class with sum method
Java
 




x
11


 
1
package com.in28minutes.junit;
2
 
          
3
public class MyMath {
4
    int sum(int[] numbers) {
5
        int sum = 0;
6
        for (int i : numbers) {
7
            sum += i;
8
        }
9
        return sum;
10
    }
11
}



Step 3: First Code and First Unit Test

  • Unit test for the sum method
Java
 




xxxxxxxxxx
1
27


 
1
package com.in28minutes.junit;
2
 
          
3
import static org.junit.Assert.assertEquals;
4
 
          
5
import org.junit.After;
6
import org.junit.AfterClass;
7
import org.junit.Before;
8
import org.junit.BeforeClass;
9
import org.junit.Test;
10
 
          
11
public class MyMathTest {
12
    MyMath myMath = new MyMath();
13
 
          
14
    // MyMath.sum
15
    // 1,2,3 => 6
16
    @Test
17
    public void sum_with3numbers() {
18
        System.out.println("Test1");
19
        assertEquals(6, myMath.sum(new int[] { 1, 2, 3 }));
20
    }
21
 
          
22
    @Test
23
    public void sum_with1number() {
24
        System.out.println("Test2");
25
        assertEquals(3, myMath.sum(new int[] { 3 }));
26
    }
27
}



Step 4: Other Assert Methods

  • assertTrue and assertFalse methods
Java
 




xxxxxxxxxx
1
18


 
1
package com.in28minutes.junit;
2
 
          
3
import static org.junit.Assert.assertEquals;
4
import static org.junit.Assert.assertTrue;
5
 
          
6
import org.junit.Test;
7
 
          
8
public class AssertTest {
9
 
          
10
    @Test
11
    public void test() {
12
        boolean condn = true;
13
        assertEquals(true, condn);
14
        assertTrue(condn);
15
        // assertFalse(condn);
16
    }
17
 
          
18
}



Step 5: Important Annotations

  • @Before @After annotations
    • Run before and after every test method in the class
  • @BeforeClass @AfterClass annotations
    • Static methods which are executed once before and after a test class
Java
 




xxxxxxxxxx
1
47


 
1
package com.in28minutes.junit;
2
 
          
3
import static org.junit.Assert.assertEquals;
4
 
          
5
import org.junit.After;
6
import org.junit.AfterClass;
7
import org.junit.Before;
8
import org.junit.BeforeClass;
9
import org.junit.Test;
10
 
          
11
public class MyMathTest {
12
    MyMath myMath = new MyMath();
13
 
          
14
    @Before
15
    public void before() {
16
        System.out.println("Before");
17
    }
18
 
          
19
    @After
20
    public void after() {
21
        System.out.println("After");
22
    }
23
 
          
24
    @BeforeClass
25
    public static void beforeClass() {
26
        System.out.println("Before Class");
27
    }
28
 
          
29
    @AfterClass
30
    public static void afterClass() {
31
        System.out.println("After Class");
32
    }
33
 
          
34
    // MyMath.sum
35
    // 1,2,3 => 6
36
    @Test
37
    public void sum_with3numbers() {
38
        System.out.println("Test1");
39
        assertEquals(6, myMath.sum(new int[] { 1, 2, 3 }));
40
    }
41
 
          
42
    @Test
43
    public void sum_with1number() {
44
        System.out.println("Test2");
45
        assertEquals(3, myMath.sum(new int[] { 3 }));
46
    }
47
}



Complete Code Example

/src/com/in28minutes/junit/MyMath.java

Java
 




xxxxxxxxxx
1
11


 
1
package com.in28minutes.junit;
2
 
          
3
public class MyMath {
4
    int sum(int[] numbers) {
5
        int sum = 0;
6
        for (int i : numbers) {
7
            sum += i;
8
        }
9
        return sum;
10
    }
11
}



/test/com/in28minutes/junit/AssertTest.java

Java
 
xxxxxxxxxx
1
18
 
1
package com.in28minutes.junit;
2
 
           
3
import static org.junit.Assert.assertEquals;
4
import static org.junit.Assert.assertTrue;
5
 
           
6
import org.junit.Test;
7
 
           
8
public class AssertTest {
9
 
           
10
    @Test
11
    public void test() {
12
        boolean condn = true;
13
        assertEquals(true, condn);
14
        assertTrue(condn);
15
        // assertFalse(condn);
16
    }
17
 
           
18
}


/test/com/in28minutes/junit/MyMathTest.java

Java
 
xxxxxxxxxx
1
47
 
1
package com.in28minutes.junit;
2
 
           
3
import static org.junit.Assert.assertEquals;
4
 
           
5
import org.junit.After;
6
import org.junit.AfterClass;
7
import org.junit.Before;
8
import org.junit.BeforeClass;
9
import org.junit.Test;
10
 
           
11
public class MyMathTest {
12
    MyMath myMath = new MyMath();
13
 
           
14
    @Before
15
    public void before() {
16
        System.out.println("Before");
17
    }
18
 
           
19
    @After
20
    public void after() {
21
        System.out.println("After");
22
    }
23
 
           
24
    @BeforeClass
25
    public static void beforeClass() {
26
        System.out.println("Before Class");
27
    }
28
 
           
29
    @AfterClass
30
    public static void afterClass() {
31
        System.out.println("After Class");
32
    }
33
 
           
34
    // MyMath.sum
35
    // 1,2,3 => 6
36
    @Test
37
    public void sum_with3numbers() {
38
        System.out.println("Test1");
39
        assertEquals(6, myMath.sum(new int[] { 1, 2, 3 }));
40
    }
41
 
           
42
    @Test
43
    public void sum_with1number() {
44
        System.out.println("Test2");
45
        assertEquals(3, myMath.sum(new int[] { 3 }));
46
    }
47
}
JUnit unit test

Published at DZone with permission of Ranga Karanam, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Mastering Unit Testing and Test-Driven Development in Java
  • Hints for Unit Testing With AssertJ
  • How to Migrate From JUnit 4 to JUnit 5 Step by Step
  • Testing Asynchronous Operations in Spring With JUnit 5 and Byteman

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!