简体   繁体   中英

JUnit Mockito with H2 Database

I am trying to write JUnit for spring boot controller using Mockito framework. I have injected the service class. I am using embedded h2 database.

When i tried to debug the written test case, I could see that it doesn't invoke the implementation class of the service method and returns empty array. I have attached debug screenshot of the controller class.

Below is the written JUnit class file:-

package com.testSpringBoot;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import com.testSpringBoot.controller.EmployeeController;
import com.testSpringBoot.dto.EmployeeDTO;
import com.testSpringBoot.repository.EmployeeRepository;
import com.testSpringBoot.service.EmployeeService;

import junit.framework.Assert;

@RunWith(SpringRunner.class)
@SpringBootTest
public class EmployeeControllerJUnit {

    private MockMvc mockMvc;

    @Mock
    private EmployeeService employeeService;

    @InjectMocks
    private EmployeeController employeeController;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);

        mockMvc = MockMvcBuilders.standaloneSetup(employeeController).build();
    }

    @Test
    public void employeeControllerTest() throws Exception {
        Mockito.when(employeeService.getAllEmployees()).thenReturn(new ArrayList<EmployeeDTO>());

        mockMvc.perform(MockMvcRequestBuilders.get("/employee/getAllEmployees"))
        .andExpect(status().isOk());
    }   
}

Debug Screenshot at controller file:-

Debug screenshot

Could you please let me know where i am making mistake.

Thanks

@Mock
private EmployeeService employeeService;

This instructs Mockito JUnit runner to create a mock of EmployeeService type. A mock does not use any implementation that you could write in your EmployeeService class (or in any of its implementations if EmployeeService is an interface). Instead, it just 'mocks' the behavior of that class.

Here, you instruct the mock to return an empty list when its getAllEmployees() method is called:

Mockito.when(employeeService.getAllEmployees()).thenReturn(new ArrayList<EmployeeDTO>());

And according to your description, it does just that.

You need to decide whether you need to mock your service or not. If not, don't use Mockito's mocks and just use your service implementation (although this will convert your test to an integration test).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM