如何为此控制器编写JUnit?

问题描述 投票:0回答:1
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);
                }
            }
        }
    }
}

我是JUnit的新手,不知道如何涵盖controllers。如果能找到一个例子,那就太好了,这样我就可以理解如何为其他控制器编写代码

java junit spring-restcontroller
1个回答
1
投票

MockMvc希望是你所追求的https://spring.io/guides/gs/testing-web/

© www.soinside.com 2019 - 2024. All rights reserved.