Unit Testing Java Student Class

JavaJavaBeginner
Practice Now

Introduction

In this project, you will learn how to write unit tests for a simple Student class using the JUnit testing framework. The project involves setting up the testing environment, writing test cases for the get and set methods of the Student class, and verifying the correctness of the implementation.

🎯 Tasks

In this project, you will learn:

  • How to import the JUnit dependency into the project
  • How to set up the StudentTest class and initialize a Student object for testing
  • How to write test cases for the get methods of the Student class
  • How to write test cases for the set methods of the Student class

🏆 Achievements

After completing this project, you will be able to:

  • Set up a testing environment using Maven and JUnit
  • Write effective unit tests for a Java class
  • Verify the correctness of the implementation using test cases
  • Apply best practices for writing maintainable and reliable code

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/ConcurrentandNetworkProgrammingGroup(["`Concurrent and Network Programming`"]) java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ConcurrentandNetworkProgrammingGroup -.-> java/working("`Working`") subgraph Lab Skills java/oop -.-> lab-300402{{"`Unit Testing Java Student Class`"}} java/working -.-> lab-300402{{"`Unit Testing Java Student Class`"}} end

Import JUnit Dependency

In this step, you will learn how to import the JUnit dependency into the project.

  1. Open the pom.xml file located in the ~/project/StudentClass directory.
  2. Add the following dependency inside the <dependencies> section:
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>

This will add the JUnit 4 dependency to the project, which is required for writing and running the unit tests.

Set Up the Student Test Class

In this step, you will learn how to set up the StudentTest class and initialize a Student object for testing.

  1. Open the StudentTest.java file located in the ~/project/StudentClass/src/test/java/com/labex directory.
  2. Add the following @Before annotated method to the StudentTest class:
private Student studentTest;

@Before
public void setUp() {
    studentTest = new Student("Tom", "male", 25);
}

This setUp() method will be executed before each test method, and it will create a new Student object with the given name, sex, and age.

Write Tests for the Get and Set Methods

In this step, you will write tests to verify the get and set methods of the Student class.

  1. Add the following test methods to the StudentTest class:
@Test
public void testGetName() {
    assertEquals("Tom", studentTest.getName());
}

@Test
public void testGetAge() {
    assertEquals(25, studentTest.getAge());
}

@Test
public void testGetSex() {
    assertEquals("male", studentTest.getSex());
}

These tests will ensure that the getName(), getAge(), and getSex() methods of the Student class are working correctly.

  1. Add the following test methods to the StudentTest class:
@Test
public void testSetName() {
    studentTest.setName("Jimmy");
    assertEquals("Jimmy", studentTest.getName());
}

@Test
public void testSetAge() {
    studentTest.setAge(26);
    assertEquals(25, studentTest.getAge()); // Age should remain unchanged
}

@Test
public void testSetSex() {
    studentTest.setSex("female");
    assertEquals("female", studentTest.getSex());
}

These tests will ensure that the setName(), setAge(), and setSex() methods of the Student class are working correctly.

Run the Test Case

To run the test case, follow these steps:

  1. In your terminal, navigate to the StudentClass project directory using the following command:
cd ~/project/StudentClass/
  1. Run the following command to execute the test case:
mvn test

The output should be similar to the following:

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.labex.StudentTest
[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.099 s - in com.labex.StudentTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.237 s
[INFO] Finished at: 2024-05-12T12:23:30Z
[INFO] ------------------------------------------------------------------------

These tests ensure that the methods of the Student class, such as getName, getAge, getSex, setName, setAge, and setSex, are functioning correctly by validating their expected behavior through assertions.

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Java Tutorials you may like