小编Sam*_*rji的帖子

when()需要一个参数,该参数必须是“模拟的方法调用”

我在Spring中为我的Controller类编写了JUnit测试用例,因为我得到了MissingMethodInvocationException,它说when()需要一个参数,而该参数必须是在when()中调用WebTarget的模拟对象时必须是``模拟的方法调用''。然后方法。

这是代码。

@Controller 
public class JobController {   

    WebTarget target = null;   
    ClientConfig config = new ClientConfig();  
    Client client = ClientBuilder.newClient(config);`
    List<ScheduleJob> scheduleJobsList = new ArrayList<ScheduleJob>();

    @RequestMapping(value = "displayScheduledJobs")
    public ModelAndView displayScheduledJobs(@ModelAttribute(value = "Server") Server server) throws JAXBException {

        List<Server> serverList = serverService.getJobServerList(user.getAccountId());
        target = client.target(getBaseURI(server));
        String xml = target.path("rest").path("getScheduledJobs").request().accept(MediaType.APPLICATION_XML).get(String.class);
        if (xml.contains("<scheduleJob>") && xml.contains("</scheduleJob>")){
            //some code
       }
        model.addObject("scheduleJobsList", scheduleJobsList);
        return model;

     }

    private static URI getBaseURI(Server server) {
        if(server.getSecureFlag().equalsIgnoreCase("N")){
            return UriBuilder.fromUri("http://"+server.getServerIp()+":"+server.getServerPort()+"/jobserver").build();
        } else {
            return UriBuilder.fromUri("https://"+server.getServerIp()+":"+server.getServerPort()+"/jobserver").build();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的测试代码。

@RunWith(MockitoJUnitRunner.class) …
Run Code Online (Sandbox Code Playgroud)

java spring unit-testing

5
推荐指数
1
解决办法
8804
查看次数

标签 统计

java ×1

spring ×1

unit-testing ×1