Mockito - Mock自动装配存储库

时间:2016-02-16 10:02:53

标签: java spring unit-testing testing mockito

我正在试图从控制器编写测试。 控制器调用调用自动装入存储库的服务。

现在这是我的班级

@RunWith(MockitoJUnitRunner.class)
public class AdminDestinationControllerTest {

private final PTUser user = new PTUser();
private MockMvc mockMvc;

@InjectMocks
private AdminDestinationController adminDestinationController;

@Mock private RoutingDestinationServiceImpl  routingDestinationService;
@Mock private PTRoutingDestinationRepository destinationRepository;

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

@Test
public void setCost_UnknownCode() throws Exception {
    when(routingDestinationService.setCost(Mockito.anyString(), Mockito.any())).thenCallRealMethod();
    when(destinationRepository.setCost(Mockito.anyString(), Mockito.any())).thenReturn(Optional.empty());
    mockMvc.perform(post("/admin/destinations/cost").content("{\"code\":\"99999\", \"cost\":\"2\"}").contentType(MediaType.APPLICATION_JSON))
        .andExpect(status().isNotFound());
}
}

但是在服务中,存储库为null。 关于我如何做的任何想法?

谢谢

0 个答案:

没有答案