- Lab
- Core Tech

Guided: Unit Testing in Java SE 17
In this hands-on lab, you will learn the JUnit basics while writing tests for an existing library application. This will include coverage on the JUnit lIfe cycle setup, cleanup, and test execution for CRUD functionality. By the end of this lab, you should be comfortable writing a variety of tests for an existing application.

Path Info
Table of Contents
-
Challenge
JUnit Life Cycle Setup
In this section, you will implement the setup phase of the testing life cycle, focusing on utilizing JUnit attributes and completing the partially implemented methods.
The file you will be working on throughout this lab is
src/test/java/com/pluralsight/BookDAOTest.java
. The objective is to create unit tests for the functionality insrc/main/java/com/pluralsight/BookDAO.java
, specifically around methods handling database CRUD operations.To start the application, execute
mvn tomcat7:run
. Note that you cannot run the application and tests concurrently, so make sure to stop the application before running tests. Once the application is running, you can access and interact with it using a web browser.To run tests, use the command
mvn test
.During the setup phase, the necessary test infrastructure is established. JUnit offers class level setup (@BeforeAll) and method level setup (@BeforeEach). Typically, resource-intensive objects like database connections are created in the class level setup, while lightweight objects, such as test objects, are reset in the method level setup.
-
Challenge
JUnit Life Cycle Cleanup
In this section, you will be implementing the cleanup phase of the testing life cycle. This will include using JUnit attributes and finishing method implementation already started for you.
For reference, the work to be done for this lab is located in
src/test/java/com/pluralsight/BookDAOTest.java
. The goal at the end of this lab will to have unit tests around the functionality found insrc/main/java/com/pluralsight/BookDAO.java
.We will be writing tests around methods that perform database CRUD operations to ensure they work correctly.
The startup script installed all dependencies with the command
mvn clean install
. If you want to start the application, runmvn tomcat7:run
. At this time. you cannot have the application running while running tests so you will need to stop the application before running tests. Once the application is started, you can see and interact with it from the web browser.To run tests going forward you can run the command
mvn test
.In the cleanup phase, the test infrastructure from the setup phase will be torn down. Just like setup, teardown also happens at class level (@AfterAll) and method level (@AfterEach).
-
Challenge
JUnit Life Cycle Test Execution for Create Operation
In this section, you will be implementing the test execution phase of the testing life cycle for our create operation. This will include using JUnit annotations and finishing method implementation already started for you.
For reference, the work to be done for this lab is located in
src/test/java/com/pluralsight/BookDAOTest.java
. The goal at the end of this lab will to have unit tests around the functionality found insrc/main/java/com/pluralsight/BookDAO.java
.We will be writing tests around methods that perform database CRUD operations to ensure they work correctly.
To run tests going forward you can run the command
mvn test
.In the test execution phase, the test execution and assertion happen. The execution result will signify a success or failure.
For reference, our test method names are following the
MethodName_StateUnderTest_ExpectedBehavior
naming convention. -
Challenge
JUnit Life Cycle Test Execution For Read Operations
In this section, you will be implementing the test execution phase of the testing life cycle for all of our read operations. This will include using JUnit annotations and finishing method implementation already started for you.
For reference, the work to be done for this lab is located in
src/test/java/com/pluralsight/BookDAOTest.java
. The goal at the end of this lab will to have unit tests around the functionality found insrc/main/java/com/pluralsight/BookDAO.java
.To run tests, you can run the command
mvn test
.For reference, our test method names are following the
MethodName_StateUnderTest_ExpectedBehavior
naming convention. -
Challenge
JUnit Life Cycle Test Execution for Update Operation
In this section, you will be implementing the test execution phase of the testing life cycle for our update operations. This will include using JUnit annotations and finishing method implementation already started for you.
For reference, the work to be done for this lab is located in
src/test/java/com/pluralsight/BookDAOTest.java
. The goal at the end of this lab will to have unit tests around the functionality found insrc/main/java/com/pluralsight/BookDAO.java
.To run tests, you can run the command
mvn test
.For reference, our test method names are following the
MethodName_StateUnderTest_ExpectedBehavior
naming convention. -
Challenge
JUnit Life Cycle Test Execution for Delete Operation
In this section, you will be implementing the test execution phase of the testing life cycle for the delete operation. This will include using JUnit annotations and finishing method implementation already started for you.
For reference, the work to be done for this lab is located in
src/test/java/com/pluralsight/BookDAOTest.java
. The goal at the end of this lab will to have unit tests around the functionality found insrc/main/java/com/pluralsight/BookDAO.java
.To run tests, you can run the command
mvn test
.For reference, our test method names are following the
MethodName_StateUnderTest_ExpectedBehavior
naming convention.
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.