服务对象没有被数据模拟?

时间:2015-09-29 10:58:42

标签: java mocking mockito springjunit4classrunner

Service object not mocked from controller testcase return empty object here is the below code 

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringApplicationConfiguration(classes = Main.class)
    @WebAppConfiguration
    @ActiveProfiles(ApplicationConstants.DEVELOPMENT_PROFILE)
    public class EmployeeControllerTest {
    @Autowired
    private WebApplicationContext   webAppContext;
    private MockMvc mockMvc;

    @Mock
    EmployeeCompositeService  employeeCompositeService;
    @InjectMocks 
    EmployeeController employeeController;

    String name = "mike";

    @Before
    public void setUp() throws Exception {
        mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build();
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testGetEmployees() throws Exception {

        Mockito.when(employeeCompositeService.getEmployeesByName(name)).thenReturn(getEmployees());
        String url = URIConstants.ROOT_CONTEXT + URIConstants.EMPLOYEE;
        MvcResult result =
        mockMvc.perform(post(url)
                        .contentType(APPLICATION_JSON_UTF8)
                        .content(convertObjectToJsonBytes(name))
                        .andExpect(status().isOk())
                        .andExpect(content().contentType(APPLICATION_JSON_UTF8))
                        .andExpect(jsonPath("$[0].employeeName").value("Mike"))
                        .andReturn();
        String jsonContent = result.getResponse().getContentAsString();
        LOGGER.debug("jsonContent: {}",jsonContent);

    }

    protected byte[] convertObjectToJsonBytes(Object object) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        return mapper.writeValueAsBytes(object);
    }

    private List<Employee> getEmployees(){
    //here is the logic to get List of employees to return. When the mockito call is invoked.
    }

    }

@RestController
@RequestMapping(value = URIConstants.ROOT_CONTEXT)
public EmployeeController{
@Autowired
private EmployeeCompositeService employeeCompositeService;

    @RequestMapping(value=URIConstants.EMPLOYEE, method=RequestMethod.POST, consumes = INPUT_FORMAT, produces = OUTPUT_FORMAT)
    public List<Employees> getEmployees(@RequestBody String name){
        return employeeCompositeService.getEmployeesByName(name); //When invoke this method it calls inner service but not returns the mocked object.

    }
}

我在旁边的EmployeeController中有一个服务调用,即

employeeCompositeService.getEmployees(String name)

所以我在EmployeeControllerTestcase中进行了模拟,即

@Mock EmployeeCompositeService  employeeCompositeService;

当我运行控制器测试用例时,它会调用更多的服务调用和存储库并命中数据库

所以我不想调用内部服务从控制器返回结果从这个调用employeeCompositeService.getEmployeesByName(name);返回。

你可以告诉我上面的代码我做错了吗谢谢你提前

1 个答案:

答案 0 :(得分:1)

创建了EmployeeCompositeService模拟对象,但未在任何地方注入。这是在测试中初始化一个类成员,但由于它没有被注入到控制器中,因此正在使用面向数据库的真实实现。要将模拟注入控制器,请将控制器声明为测试中的类成员,并使用org.mockito.InjectMocks注释对其进行注释。这将使用@Mock注释从测试中获取类成员,并将它们注入到实现相同接口的控制器字段中。

下一步是通过将控制器指定为MockMvc将控制器注入mockMvc = MockMvcBuilders.standaloneSetup(employeeController).build()对象。