{{announcement.body}}
{{announcement.title}}

JUnit Tutorial for Beginners in 5 Steps

DZone 's Guide to

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.

· Java Zone ·
Free Resource

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
 


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

Java
 
Topics:
java, spring boot, springbootunittesting, tutorial

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

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}