Unit Testing in Spring Boot: DAO, Service, and Controller With the JDBC

DZone 's Guide to

Unit Testing in Spring Boot: DAO, Service, and Controller With the JDBC

Need help with unit testing?

· Performance Zone ·
Free Resource

In this unit testing example with Spring Boot, I am going to useLabstatOutputReportDao, LabstatService, andLabstatController.

For DAO:

@Transactional means rollback on the transaction after testing. Additionally, @SpringBootTest means that we are running unit testing with the Spring Boot feature.

Note: for DAO, we directly connect to DB for testing.

public class LabstatOutputReportDaoTest {

private static final Logger log = LoggerFactory.getLogger(LabstatOutputReportDaoTest.class);

ILabstatOutputReportDao labOutputDao;

public void getAllLabOutputTest() {


if(labOutputDao.getAllLabOutput().isPresent()) {
List<Map<String, Object>> actualList = labOutputDao.getAllLabOutput().get();

For service, we are using Mokito for mocking beans. We use @MockBean for mocking the DAO. And then, we use @TestConfiguration to provide a mock bean for the service.

public class LabstatServiceTest {
  private static final Logger log = LoggerFactory.getLogger(LabstatServiceTest.class);

static class LAUSUtilityServiceTestConfiguration{

public LabstatService labstatService() {
return new LabstatService();

private ILabstatOutputReportDao labstatOutputReportDao;

private LabstatService labstatSrvc;

public void setUp() throws SQLException {



public void getAllLabOutputTest() {

log.info("unit test getAllLabOutputTest()...");

int actSize = labstatSrvc.getAllLabOutput().size();

int expSize = 2;



  private Optional<List<Map<String, Object>>> getAllLabOutputMockReturn(){

List<Map<String, Object>> list = new ArrayList<>();
list = IntStream.range(1, 3).mapToObj((idx) ->{
Map<String, Object> map = new HashMap<>();
map.put(String.valueOf(idx), "TASKNUM"+idx*10);

return map;


return Optional.ofNullable(list);


For the controller, we will be using @wemvctest for controller testing. Additionally, we will be using jsonPath for testing return if return as:

@WebMvcTest(value = LabstatController.class, secure = false)
public class LabstatControllerTest {
private static final Logger log = LoggerFactory.getLogger(LabstatControllerTest.class);

private MockMvc labMVc;

private LabstatService labstatSrvc;

public void setup() {

public void testLabCtrlGetStates() throws Exception {
// add more testing realted to json return in controller later on

log.info("unit testing controller testLabCtrlGetStates()...");

// test the first data in states should be st1
.andExpect(jsonPath("$.states.states[0].name", is("st1")));

  private Map<String, Object> getAllStatesServiceMockReturn() {
Map<String, Object> stateMap = new HashMap<>();
List<State> stateList = new ArrayList<>();

for (int i = 1; i < 10; i++) {
State tmpST = new State();
tmpST.setName("st" + i);
tmpST.setStateCode(String.valueOf(10 + i));
tmpST.setStateNum(String.valueOf(100 + i));

stateMap.put("states", stateList);

return stateMap;

Happy testing!

controller, dao, java, jdbc, performance, service, spring boot, spring boot 2.0, testing, unit testing

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}