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
StudentTestclass and initialize aStudentobject for testing - How to write test cases for the
getmethods of theStudentclass - How to write test cases for the
setmethods of theStudentclass
🏆 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
Import JUnit Dependency
In this step, you will learn how to import the JUnit dependency into the project.
- Open the
pom.xmlfile located in the~/project/StudentClassdirectory. - 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.
- Open the
StudentTest.javafile located in the~/project/StudentClass/src/test/java/com/labexdirectory. - Add the following
@Beforeannotated method to theStudentTestclass:
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.
- Add the following test methods to the
StudentTestclass:
@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.
- Add the following test methods to the
StudentTestclass:
@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:
- In your terminal, navigate to the
StudentClassproject directory using the following command:
cd ~/project/StudentClass/
- 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.



