🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Overview
In this post, we will see the JUnit 4 basic template that all the JUnit test cases follow. This post also explains the basic usage of JUnit annotations.
The following templates are a good starting point. Copy/paste and edit these templates to suit your coding style.
2. Basic Test Template
import org.junit.*;
import static org.junit.Assert.*;
public class SampleTest {
private java.util.List emptyList;
/**
* Sets up the test fixture.
* (Called before every test case method.)
*/
@Before
public void setUp() {
emptyList = new java.util.ArrayList();
}
/**
* Tears down the test fixture.
* (Called after every test case method.)
*/
@After
public void tearDown() {
emptyList = null;
}
@Test
public void testSomeBehavior() {
assertEquals("Empty list should have 0 elements", 0, emptyList.size());
}
@Test(expected=IndexOutOfBoundsException.class)
public void testForException() {
Object o = emptyList.get(0);
}
}
3. Basic Test Template with Fixture
You can add a @BeforeClass annotation to a method to be run before all the tests in a class, and a @AfterClass annotation to a method to be run after all the tests in a class.Here's a simple example:
package junit;
import org.junit.*;
import static org.junit.Assert.*;
import java.util.*;
public class SimpleTest {
private Collection collection;
@BeforeClass
public static void oneTimeSetUp() {
// one-time initialization code
}
@AfterClass
public static void oneTimeTearDown() {
// one-time cleanup code
}
@Before
public void setUp() {
collection = new ArrayList();
}
@After
public void tearDown() {
collection.clear();
}
@Test
public void testEmptyCollection() {
assertTrue(collection.isEmpty());
}
@Test
public void testOneItemCollection() {
collection.add("itemA");
assertEquals(1, collection.size());
}
}
Given this test, the methods will execute in the following order:
oneTimeSetUp()
setUp()
testEmptyCollection()
tearDown()
setUp()
testOneItemCollection()
tearDown()
oneTimeTearDown()
4. Real-World Example using JUnit Framework
Let's understand a little bit more about using JUnit tests in a real-world example.
Consider we have a School Management System application and we want to write a sample test cases for the School entity.
In this example, we will see the sample CRUD JUnit test cases for the School entity:
public class SchoolServiceImplTest {
private SchoolService schoolService;
/**
* This method initialize all domain objects required for test methods.
*/
@Before
public final void setUp() throws Exception {
// Instantiates a School instance;
school = new School();
school.setName("sms School");
school.setAddress("sms School");
school.setContactNo("0123456789");
school.setFaxNo("0123456789");
school.setWebsite("javaguides.net");
school.setStartedDate(date);
school.setModifiedTime(date);
}
@Test
public final void testAddSchool() throws Exception {
School newSchool = schoolService.addSchool(school);
assertNotNull("School Type object should not null ", newSchool);
}
@Test
public final void testUpdateSchool() {
School newSchool = schoolService.addSchool(school);
assertNotNull(newSchool);
newSchool.setContactNo("0145785545");
schoolService.updateSchool(newSchool);
}
@Test
public final void testFindSchool() throws AkuraAppException {
School newSchool = schoolService.addSchool(school);
assertNotNull(newSchool);
School findSchool = schoolService.findSchool(
newSchool.getSchoolId());
assertNotNull("School Type object should not null ", findSchool);
}
@Test
public final void testGetSchoolList() throws AkuraAppException {
School newSchool = schoolService.addSchool(school);
assertNotNull(newSchool);
List<School> schoolList = schoolService.getSchoolList();
}
@After
public final void teardown() throws SchoolNotFoundException {
schoolDao.delete(school);
}
}
4. Conclusion
In this post, we have learned about JUnit basic template from which we can edit and write test cases. We also have seen the usage of the fixture and written test cases to a real-world example of School Management System application.The source code of this article available on my GitHub repository at https://github.com/RameshMF/junit-developer-guide.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment