Java 如何为此控制器编写JUnit? private static final Logger Logger=LoggerFactory.getLogger(InfoController.class); @RequestMapping(value=“/version”,method={RequestMethod.GET,RequestMethod.POST}) @应答器 公共映射getVersion()引发IOException{ 最终字符串versionKey=“Version”; 返回Collections.singletonMap(versionKey,loadManifest().getProperty(versionKey)); } @RequestMapping(value=“/info”,method={RequestMethod.GET,RequestMethod.POST}) @应答器 @SuppressWarnings(“未选中的强制转换”) 公共映射getInfo()引发IOException{ 返回Collections.checkedMap((Map)loadManifest(),String.class,String.class); } 私有属性loadManifest()引发IOException{ final InputStream stream=this.getClass().getResourceAsStream(“/META-INF/MANIFEST.MF”); 试一试{ 最终属性清单=新属性(); 装载(流); 返回舱单; }最后{ if(流!=null){ 试一试{ stream.close(); }捕获(IOE异常){ LOGGER.error(e.getMessage(),e); } } } } }

Java 如何为此控制器编写JUnit? private static final Logger Logger=LoggerFactory.getLogger(InfoController.class); @RequestMapping(value=“/version”,method={RequestMethod.GET,RequestMethod.POST}) @应答器 公共映射getVersion()引发IOException{ 最终字符串versionKey=“Version”; 返回Collections.singletonMap(versionKey,loadManifest().getProperty(versionKey)); } @RequestMapping(value=“/info”,method={RequestMethod.GET,RequestMethod.POST}) @应答器 @SuppressWarnings(“未选中的强制转换”) 公共映射getInfo()引发IOException{ 返回Collections.checkedMap((Map)loadManifest(),String.class,String.class); } 私有属性loadManifest()引发IOException{ final InputStream stream=this.getClass().getResourceAsStream(“/META-INF/MANIFEST.MF”); 试一试{ 最终属性清单=新属性(); 装载(流); 返回舱单; }最后{ if(流!=null){ 试一试{ stream.close(); }捕获(IOE异常){ LOGGER.error(e.getMessage(),e); } } } } },java,junit,spring-restcontroller,Java,Junit,Spring Restcontroller,我不熟悉JUnit,不知道如何涵盖控制器。如果能给我举个例子,这样我就可以理解如何为其他控制器编写代码了,那就太好了。MockMvc有望成为您的目标 private static final Logger LOGGER = LoggerFactory.getLogger(InfoController.class); @RequestMapping(value = "/version", method = {RequestMethod.GET, RequestMethod.POST})

我不熟悉
JUnit
,不知道如何涵盖
控制器。如果能给我举个例子,这样我就可以理解如何为其他控制器编写代码了,那就太好了。

MockMvc有望成为您的目标

private static final Logger LOGGER = LoggerFactory.getLogger(InfoController.class);
    @RequestMapping(value = "/version", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, String> getVersion() throws IOException {
        final String versionKey = "Version";
        return Collections.singletonMap(versionKey, loadManifest().getProperty(versionKey));
    }
    @RequestMapping(value = "/info", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    @SuppressWarnings("unchecked cast")
    public Map<String, String> getInfo() throws IOException {
        return Collections.checkedMap((Map) loadManifest(), String.class, String.class);
    }
    private Properties loadManifest() throws IOException {
        final InputStream stream = this.getClass().getResourceAsStream("/META-INF/MANIFEST.MF");
        try {
            final Properties manifest = new Properties();
            manifest.load(stream);
            return manifest;
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e){
                    LOGGER.error(e.getMessage(),e);
                }
            }
        }
    }
}