VOOZH about

URL: https://dzone.com/articles/junit-tutorial-for-beginners-in-5-steps

โ‡ฑ JUnit Tutorial for Beginners in 5 Steps


Related

  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 Jul. 27, 20 ยท Tutorial
Likes
Comment
Save
11.2K 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. 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
  • Effective Engineering Feedback: Software Testing
  • The LLM Selection War Story: Part 2 - The Six LLM Failure Archetypes That Will Wreck Your Production System

Partner Resources

ร—

Comments

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

Let's be friends: