是否可以使用 MockMvc 解决一种特定形式
Is it possible to address one specific form using MockMvc
你好,
我想问问是否可以使用 MockMvc 在 html 模板中处理一种特定形式。我有一个包含 3 种形式的模板,但我无法处理其中一种。
这是我的测试:
@Test
public void CreateNewIllMessageTest() throws Exception {
initializeDummyTourWithDummyGuide();
User user = userRepository.findUserByName("dummy");
String moreInf = "moreInformation";
mvc.perform(get("/tourmanagement").with(user(user.getUsername()).roles("EMPLOYEE"))).
andExpect(status().isOk()).
andExpect(view().name("tourmanagement")).
andExpect(model().attributeExists("illnessform")).
andExpect(model().attribute("illnessform", hasProperty("customer", is(nullValue())))).
andExpect(model().attribute("illnessform", hasProperty("moreInf", isEmptyOrNullString())));
mvc.perform(post("/tourmanagement").with(user(user.getUsername()).roles("EMPLOYEE")).
param("customer", user.getAccount().getId().toString()).
param("moreInf", moreInf)
).
andExpect(status().is3xxRedirection());
}
这是模板:
<form action="#" th:object="${illnessform}" method="post" name="illnessform"
th:action="@{/tourmanagement/ill(tour=${tourid}, start=${startDate}, end=${endDate}, tourname=${tour.name})}">
<div id="ill" style="display: none" class="form-group">
<label for="illselect" th:text="#{tourmanagement.whoisill}">Which Tourist Is Ill?</label>
<select class="form-control" id="illselect" name="customer">
<option th:each="customer : ${customerList}" th:value="${customer.account.id}">
<p th:text="${customer.account.id}"></p>
</option>
</select>
<label for="illInf" th:text="#{tourmanagement.moreinfs}">More Information:</label>
<input id="illInf" type="text" class="form-control" required name="moreInf" th:field="*{moreInf}">
<button class="btn btn-info" name="submit" type="submit" th:text="#{contact.submit}">Submit</button>
</div>
</form>
<form action="#" th:object="${insuranceform}" method="post"
th:action="@{/tourmanagement/ins(tour=${tourid}, start=${startDate}, end=${endDate}, tourname=${tour.name})}">
<div id="ins" style="display: none" class="form-group">
<label for="inssellect" th:text="#{tourmanagement.whatkindofins}">What Kind Of Insurance Case?</label>
<select class="form-control" id="inssellect" name="caseType" th:field="*{caseType}">
<option th:text="#{tourmanagement.theft}" th:value="#{tourmanagement.theft}">Theft</option>
<option th:text="#{tourmanagement.accident}" th:value="#{tourmanagement.accident}">Accident</option>
</select>
<label for="inssellecttourist" th:text="#{tourmanagement.whichtourist}">Which Tourist Did It Happen
To?</label>
<select class="form-control" id="inssellecttourist" name="customer" th:field="*{customer}">
<option th:each="customer : ${customerList}" th:value="${customer.account.id}">
<p th:text="${customer.account.id}"></p>
</option>
</select>
<label for="insInf" th:text="#{tourmanagement.moreinfs}">More Information:</label>
<input id="insInf" type="text" class="form-control" required name="moreInf" th:field="*{moreInf}">
<button class="btn btn-info" type="submit" th:text="#{contact.submit}">Submit</button>
</div>
</form>
如您所见,我有多个表格,我想测试第一个。我得到的错误是:
java.lang.AssertionError: Range for response status value 405
预期:重定向
实际:CLIENT_ERROR
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:81)
at org.springframework.test.web.servlet.result.StatusResultMatchers.match(StatusResultMatchers.java:107)
at org.springframework.test.web.servlet.MockMvc.andExpect(MockMvc.java:171)
at kickstart.Message.MessageIntegrationTest.CreateNewIllMessageTest(MessageIntegrationTest.java:145)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
我非常感谢任何帮助或测试这些表格的替代方法。
你好,亚历克斯
服务器(在本例中为 MockMVC)对表单一无所知。表单发送不同的请求,这些请求必须具有您的服务器用来区分它们的特征。这些特征必须与测试中的请求一起发送。
你好,
我想问问是否可以使用 MockMvc 在 html 模板中处理一种特定形式。我有一个包含 3 种形式的模板,但我无法处理其中一种。
这是我的测试:
@Test
public void CreateNewIllMessageTest() throws Exception {
initializeDummyTourWithDummyGuide();
User user = userRepository.findUserByName("dummy");
String moreInf = "moreInformation";
mvc.perform(get("/tourmanagement").with(user(user.getUsername()).roles("EMPLOYEE"))).
andExpect(status().isOk()).
andExpect(view().name("tourmanagement")).
andExpect(model().attributeExists("illnessform")).
andExpect(model().attribute("illnessform", hasProperty("customer", is(nullValue())))).
andExpect(model().attribute("illnessform", hasProperty("moreInf", isEmptyOrNullString())));
mvc.perform(post("/tourmanagement").with(user(user.getUsername()).roles("EMPLOYEE")).
param("customer", user.getAccount().getId().toString()).
param("moreInf", moreInf)
).
andExpect(status().is3xxRedirection());
}
这是模板:
<form action="#" th:object="${illnessform}" method="post" name="illnessform"
th:action="@{/tourmanagement/ill(tour=${tourid}, start=${startDate}, end=${endDate}, tourname=${tour.name})}">
<div id="ill" style="display: none" class="form-group">
<label for="illselect" th:text="#{tourmanagement.whoisill}">Which Tourist Is Ill?</label>
<select class="form-control" id="illselect" name="customer">
<option th:each="customer : ${customerList}" th:value="${customer.account.id}">
<p th:text="${customer.account.id}"></p>
</option>
</select>
<label for="illInf" th:text="#{tourmanagement.moreinfs}">More Information:</label>
<input id="illInf" type="text" class="form-control" required name="moreInf" th:field="*{moreInf}">
<button class="btn btn-info" name="submit" type="submit" th:text="#{contact.submit}">Submit</button>
</div>
</form>
<form action="#" th:object="${insuranceform}" method="post"
th:action="@{/tourmanagement/ins(tour=${tourid}, start=${startDate}, end=${endDate}, tourname=${tour.name})}">
<div id="ins" style="display: none" class="form-group">
<label for="inssellect" th:text="#{tourmanagement.whatkindofins}">What Kind Of Insurance Case?</label>
<select class="form-control" id="inssellect" name="caseType" th:field="*{caseType}">
<option th:text="#{tourmanagement.theft}" th:value="#{tourmanagement.theft}">Theft</option>
<option th:text="#{tourmanagement.accident}" th:value="#{tourmanagement.accident}">Accident</option>
</select>
<label for="inssellecttourist" th:text="#{tourmanagement.whichtourist}">Which Tourist Did It Happen
To?</label>
<select class="form-control" id="inssellecttourist" name="customer" th:field="*{customer}">
<option th:each="customer : ${customerList}" th:value="${customer.account.id}">
<p th:text="${customer.account.id}"></p>
</option>
</select>
<label for="insInf" th:text="#{tourmanagement.moreinfs}">More Information:</label>
<input id="insInf" type="text" class="form-control" required name="moreInf" th:field="*{moreInf}">
<button class="btn btn-info" type="submit" th:text="#{contact.submit}">Submit</button>
</div>
</form>
如您所见,我有多个表格,我想测试第一个。我得到的错误是:
java.lang.AssertionError: Range for response status value 405
预期:重定向 实际:CLIENT_ERROR
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:81)
at org.springframework.test.web.servlet.result.StatusResultMatchers.match(StatusResultMatchers.java:107)
at org.springframework.test.web.servlet.MockMvc.andExpect(MockMvc.java:171)
at kickstart.Message.MessageIntegrationTest.CreateNewIllMessageTest(MessageIntegrationTest.java:145)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
我非常感谢任何帮助或测试这些表格的替代方法。
你好,亚历克斯
服务器(在本例中为 MockMVC)对表单一无所知。表单发送不同的请求,这些请求必须具有您的服务器用来区分它们的特征。这些特征必须与测试中的请求一起发送。