Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未调用单元测试服务方法_Java_Mockito - Fatal编程技术网

Java 未调用单元测试服务方法

Java 未调用单元测试服务方法,java,mockito,Java,Mockito,我要做一个单元测试。但是我最终得到了500个错误,因此我设置了断点并在调试模式下运行测试。最后我发现服务方法没有被调用并返回null。 我只是想知道为什么它跳过了articleService.getInfo,然后使对象变为null,最后得到500个错误 @RunWith(SpringRunner.class) @SpringBootTest(classes = Controller.class) @WebAppConfiguration public class ControllerTest {

我要做一个单元测试。但是我最终得到了500个错误,因此我设置了断点并在调试模式下运行测试。最后我发现服务方法没有被调用并返回null。 我只是想知道为什么它跳过了
articleService.getInfo
,然后使对象变为null,最后得到500个错误

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Controller.class)
@WebAppConfiguration
public class ControllerTest {
    
    private MockMvc mockMvc;
   
    @MockBean
    private TestService testService;
    
    
    @Autowired
    private WebApplicationContext wac;
    
    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

@Test
public void testList() throws Exception{
    ObjectMapper mapper = new ObjectMapper();
    String jsonStr = Files.readString(Paths.get("src/test/resources/data/list.json"));
    BookMarkResponse response = mapper.readValue(jsonStr, BookMarkResponse.class);
    Mockito.when(bookmarkService.getDocument(Mockito.anyString(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(),
            Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(response);
    
    MvcResult result = mockMvc.perform(get("/getData")  
          .param("Id",Id))
    .andExpect(MockMvcResultMatchers.status().isOk())
    .andDo(MockMvcResultHandlers.print())
    .andReturn();
    assertNotNull(result.getResponse().getContentAsString());
    }


@RequestMapping(method = RequestMethod.GET, value="/getData")
@ResponseBody
public RandomList getList(@RequestParam("Id") int Id) {
    Response bookmarkTest = new Response();
    cTest = bookmarkService.getDocument(string, string, string, string, string, string, string);
    if(cTest.getResponseStatus() != 1){
        bkmark.setCode("Error");
        return bkmark;
    }
    List<NBookMark> nbookMarks = new ArrayList<NBookMark>();
    if( cTest.getTotal() > 0){
        nbookMarks = convert2List(cTest);
        bkmark.setCode("OK");
    }
    bkmark.setBookmarks(nbookMarks);
    return bkmark;
}

public List<NBookMark> convert2List(BookMarkResponse input) {
    List<NBookMark> Bookmarks = new ArrayList<NBookMark>();
    List<BookMarkDocument> bookmarks = input.getBookmarks();
    for(BookMarkDocument item: bookmarks) {
        NBookMark tempMark = new NBookMark();
        tempMark.setArticleId(Long.toString(item.getArticleId()));
        Document object = articleService.getInfo(articleFolderPath, Long.toString(item.getArticleId()));
        if(bkMarksource == null) { //this line the object is keep null, cannot pass the null checking
            continue; 
        }

//Do some null checking here, but cannot passed, since the object get null

是否可以使用Mockito.when…thenReturn()。调用实际函数

Mockito.when(articleService.getInfo(Mockito.anyString(),Mockito.anyString())).thenReturn(articleService.getInfo("test","test")); 

您没有展示如何创建articleService,因此我只能假设它是一个模拟对象

默认情况下,未模拟的方法不调用原始方法,它们只返回null。要让它调用原始方法,需要执行以下操作:

而不是

ArticleService articleService = Mockito.mock(ArticleService.class)
使用参数Mockito.CALLS\u REAL\u方法创建模拟对象

ArticleService articleService = Mockito.mock(ArticleService.class, Mockito.CALLS_REAL_METHODS)

这不是一门模拟课。我只是展示了类的代码,感谢如何构建测试(您是否使用了任何特殊的测试注释,如@WebMvcTest、@SpringBootTest?您是否使用了任何运行程序/扩展?。您如何构建MockMvc?。bookmarkService是如何初始化的?)所有这些都有细节,没有它们,这纯粹是猜测。
ArticleService articleService = Mockito.mock(ArticleService.class, Mockito.CALLS_REAL_METHODS)