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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Mastering Unit Testing and Test-Driven Development in Java
  • Hints for Unit Testing With AssertJ
  • Testing Business Programs Without Constructing Domain Objects
  • Why Standard Test Automation Misses the Failures That Matter in AI Agent Systems

Trending

  • Beyond the Wall of Charts: How Advanced Observability Displaced the Telemetry Dashboard
  • Why Role-Based Access Control Fails for AI Agents — and How to Fix It With Memory Lineage Engines
  • The Permission Was Valid. The Action Was Not Authorized.
  • Moving 900+ GB of Prometheus Without a Cutover Weekend

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Jul. 27, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
11.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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. 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
  • Testing Business Programs Without Constructing Domain Objects
  • Why Standard Test Automation Misses the Failures That Matter in AI Agent Systems

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook