是否可以使用MockMvc解决一个特定的表单

时间:2017-12-07 14:00:54

标签: java spring forms junit 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$5.match(StatusResultMatchers.java:107)
at org.springframework.test.web.servlet.MockMvc$1.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$1.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$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.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)

我真的很感激任何帮助或替代方法来测试这些表格。

问候,Alex

1 个答案:

答案 0 :(得分:0)

服务器(在本例中为MockMVC)对表单一无所知。表单发送不同的请求,这些请求必须具有服务器用来区分它们的特征。这些特征必须与您的测试中的请求一起发送。

相关问题