问题标签 [mockmvc]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
spring-mvc - Spring MockMvc 和异步控制器的 HTTP 状态码
当我的控制器具有异步 servlet 特性时,如何验证/测试 MockMvc 中的 500 内部服务器错误?
我正在为我的 REST 端点编写单元测试用例,作为测试用例的一部分,我需要验证服务器是否将 500 内部错误作为 http 代码发送并带有适当的错误消息。
这是我基于 Spring Boot 的应用程序:(为了更好的可读性,所有导入都被省略了)
这是我的 MovkMvc JUnit 测试用例:
下面是控制台打印:
从上面的日志中可以明显看出,MockMvc 将 http 状态代码返回为 200 而不是 500。错误消息很好。
spring - Spring Boot MockMVC 测试不加载 Yaml 文件
我在类路径根目录(src/main/resources/)的 application.yml 文件中有我的配置。当我正常启动应用程序时,配置加载正常。但是在我的测试中,application.yml 文件根本没有加载。
我的测试标题如下所示:
配置类:
当我调试应用程序时,我看到 yml 文件在 ConfigFileApplicationListener 中加载,但在测试中未调用 ConfigFileApplicationListener。
spring - Spring unit test 404\unknown url in mock mvc
I would like to test that when an unknown url is requested and a 404 error is generated that my web app actually redirects to the right place.
I havent been able to get this working, I think because tomcat is handling the 404 errors so the forwardedUrl is always null for the tests. I know this works in reality because if I enter some rubbish into the url my app does redirect to my custom page.
My unit test looks like:
My web.xml configuration is:
The mapping for /static is defined in my spring config like:
Ultimately I would like to mock a request to an unknown url and then check that the page being returned is /static/error/Sorry.html.
Am I doing something wrong or is this not the way to handle 404 etc in spring? The check of the forwarded url in the unit test is always null.
A slightly different question but related all the same is, at what point does the tomcat error handling get invoked over and above the spring controller advice handling?
java - Spring MockMvcResultMatchers jsonPath 低于/大于
我正在使用 MockMvcResultMatchers 来测试我的控制器类。
这是一个示例代码
但是我如何将 $.total 值与数字进行比较?
我的意思是,有没有办法找出 $.total > 0?
junit - 用 jsonpath 和 mockito 模拟
知道为什么这不能编译,“.andExpect(JsonPath("$.title", is("Test Blog Entry")))" 导致了 eclipse luna 中的问题。没有它就可以正常工作。这是一个 springmvc 教程应用程序,pom 也附在下面。包 com.spring.angular.tutorial.web;
}
pom是:
http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 spring_angular_tutorial spring_angular_tutorial 0.0.1-SNAPSHOT 战争
java - 如何测试 JSON 路径是否不包含特定元素,或者该元素是否存在则为空?
我一直在为一个简单的 Spring Web 应用程序编写一些简单的单元测试例程。当我在资源的 getter 方法上添加 @JsonIgnore 注释时,生成的 json 对象不包含相应的 json 元素。因此,当我的单元测试例程尝试测试这是否为空(这是我的情况的预期行为,我不希望密码在 json 对象中可用)时,测试例程会遇到异常:
java.lang.AssertionError:JSON 路径没有值:$.password,异常:路径没有结果:$['password']
这是我写的单元测试方法,用 is(nullValue()) 方法测试“密码”字段:
我也尝试过使用 jsonPath().exists() 得到类似的异常,指出路径不存在。我正在分享更多代码片段,以便整个情况变得更具可读性。
我正在测试的控制器方法如下所示:
我正在使用 spring hatos 资源组装器将实体转换为资源对象,这是我的资源类:
我理解为什么这会出现异常,在某种程度上,测试是成功的,它找不到密码字段。但我想做的是,运行这个测试以确保该字段不存在,或者如果存在,它包含空值。我怎样才能做到这一点?
堆栈溢出中有一个类似的帖子: Hamcrest with MockMvc: check that key exists but value may be null
就我而言,该字段也可能不存在。
作为记录,这些是我正在使用的测试包的版本:
提前致谢。
[编辑] 更准确地说,你必须为一个实体编写一个测试,你知道其中一些字段需要为空或空或者甚至不应该存在,并且你实际上并没有通过代码来查看如果在属性顶部添加了 JsonIgnore。你希望你的测试通过,我该怎么做。
请随时告诉我,这根本不实用,但仍然很高兴知道。
[编辑] 上述测试通过以下较旧的 json-path 依赖项成功:
[编辑] 在阅读了 spring 的 json 路径匹配器的文档后,找到了一个适用于最新版本 jayway.jasonpath 的快速修复。
java - 如何使用 Spring MockMVC 将 JSON 编码为请求参数
我已经尝试了几个小时将 JSON 编码为我正在使用 Spring 的 MockMVC 编写的测试的请求参数,但没有运气。
我的测试看起来像
但是到达我的控制器的字符串仍然是编码的(“%7B%22key%22:%22value%22%7D”),因此不能反序列化为 JSON。
为了让 Spring 理解编码参数,我缺少什么?
谢谢你的帮助
spring - Spring integration test with mockmvc throws org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
I have a weird problem with a spring integrationtest. I have a working controller and application that runs on tomcat. However in my integration test I get the following stacktrace:
My test looks like this:
And my controller method:
dto:
my gradle.build
To make things more interesting I also have a unit test for the controller that works which uses standalone setup:
If anyone has any suggestions on how to move forward it would be extremely appreciated!!
SOLVED - I had an excludeFilters={@Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class) in my Root config to get pass jsp runtime exceptions that was thrown. When I removed that and included a serverruntime on my classpath things started to work.
Thanks Karl
java - 如何在 Spring Rest 中使用 MockMVC 测试 Map 参数
在Spring Rest中,我有一个RestController公开此方法:
我想使用Spring中的MockMVC测试此方法:
问题是我收到500 HTTP 错误代码。我很确定这是因为我使用Map作为控制器的参数(我尝试将其更改为 String 并且它有效)。
问题是:如何在我的RestController中有一个Map in 参数,并使用MockMVC正确测试它?
谢谢你的帮助。
java - 在 Spring 中测试控制器?
我想通过调用一些控制器来编写功能测试。
调用 perform/1 后,用户模型存储在会话中:
我想在同一测试中的 perform/1 之后立即调用 perform/2 但似乎 user = null 。这是正常行为吗?
我怎样才能做到这一点?如何使用 Spring 控制器编写功能测试?