java - 如何为这个 Controller 编写JUnit?

标签 java junit spring-restcontroller

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 的新手,不知道如何覆盖 Controller 。如果能得到一个例子那就太好了,这样我就可以理解如何为其他 Controller 编写

最佳答案

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

关于java - 如何为这个 Controller 编写JUnit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59099598/

相关文章:

java - 如果我的比较器在忙于向上或向下冒泡时抛出异常,我的 PriorityQueue 会发生什么情况?

java - Mockito 吞下堆栈跟踪?

java - 上传formdata到远程tomcat服务器时multipart为null

java - 在 TabLayout 中滑动选项卡时 fragment 不会刷新

java - 如何生成随机的亮色?

java - 如何使用java代码打开Windows文件资源管理器并突出显示指定文件?

android - 使用 ServiceTestCase 访问 JUnit 的资源

java - Ant 构建 junitreporting 时出现错误

java - Spring rest Controller "MultipartFile[] multipartFiles"总是接收 [] 或空文件

java - Spring Boot : Get JSON body when RESTFul error (404, 403, 500) 发生