Unable to test controller with jUnit/Springboot multiple submit buttons

Asked 1 years ago, Updated 1 years ago, 292 views

I am currently creating the springboot application and as the title suggests, I have placed several submit buttons on the form.
I'd like to do a single test on the controller method, but the request doesn't go well and I get a 403 error.
I want the test code to be able to successfully throw a request without a 403 error, but I don't know how to resolve it.

Tried

According to the reference article below, if multiple submit buttons are placed on the form, the submit button value with the name attribute will also be in the request body (I also checked with Google Chrome developer tool), so I created ExampleFormEx1/ExampleFormEx2 for testing, but it didn't work.

reference:
Spring supports multiple submit buttons

I would appreciate it if you could tell me how to fix it.

environment:

  • Java11
  • SpringBoot 2.3.6
  • Thymeleaf

Source Code

ExampleController.java

@Controller
public classExampleController {
    @ GetMapping("/")
    public init() {

        // ...handling...

        return "example.html";
    }

    @PostMapping(value="/", params="ex1")
    public postEx1(ExampleForm form) {

        // ...handling...

        return "example.html";
    }

    @PostMapping(value="/", params="ex2")
    public postEx2 (ExampleForm form) {

        // ...handling...

        return "example.html";
    }
}

ExampleForm.java

@Getter
@Setter
public classExampleForm{
    private String fullName;
    private Integerage;
}

example.html

<body>
  <form action="/"method="POST">
    <input type="text" name="fullName"/>
    <input type="number" name="age"/>
    <button type="submit" name="ex1">send to ex1</button>
    <button type="submit" name="ex2">Send to ex2</button>
  </form>
</body>

ExampleControllerTest.java

@SpringBootTest (webEnvironment=WebEnvironment.DIFINED_PORT)
@ AutoConfigureMockMvc
public classExampleControllerTest{
    @Autowired
    private MockMvc mockMvc;

    @ Test
    public void postEx1Test(){
        ExampleFormEx1 form = new ExampleFormEx1();
        form.setFullName("Test Taro");
        form.setAge(20);
        form.setEx1("");
        String requestForm = new ObjectMapper().writeValueAsString(form)

        MockHttpServletRequestBuilder request = MockMvcRequestBuilder
                .post("/")
                .content(requestForm)
                .accept("application/json; charset=UTF-8")
                .contentType("MediaType.APPLICATION_JSON");
        MvcResult result=mockMvc.perform(request)
                .andExcept(status().isOk())
                .andExcept(view.name("example.html"))
                .andReturn();

        // ...test processing...

    }

    @ Test
    public void postEx2Test(){
        ExampleFormEx2form = new ExampleFormEx2();
        form.setFullName("Test Hanako");
        form.setAge(30);
        form.setEx2("");
        String requestForm = new ObjectMapper().writeValueAsString(form)

        MockHttpServletRequestBuilder request = MockMvcRequestBuilder
                .post("/")
                .content(requestForm)
                .accept("application/json; charset=UTF-8")
                .contentType("MediaType.APPLICATION_JSON");
        MvcResult result=mockMvc.perform(request)
                .andExcept(status().isOk())
                .andExcept(view.name("example.html"))
                .andReturn();

        // ...test processing...

    }
}

ExampleFormEx1.java

@Getter
@Setter
public classExampleFormEx1 extensionsExampleForm{
    private String ex1;
}

ExampleFormEx2.java

@Getter
@Setter
public classExampleFormEx2 extensionsExampleForm{
    private String ex2;
}

java spring-boot thymeleaf junit mockito

2022-09-30 21:59

1 Answers

When submitting a form, the Content-Type is not application/json but application/x-www-form-urlencoded.
Also, you can check the request body that needs to be set in Chrome devtools (press the view source button to view raw data).

mockMvc.perform(post("/")
    .contentType(MediaType.APPLICATION_FORM_URLENCODED)
    // // https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.html#content-java.lang.String-
    .content("fullName=%E3%83%86%E3%82%B9%E3%83%E8%E5%A4%E9%83%E&age=20&ex1=")
    // Or use param().
    // .param("fullName", "Test Taro")
    // .param("age", "20")
    // .param("ex1", "")
    )
    .andExpect(status().isOk())
    .andExpect(view().name("example.html"));


2022-09-30 21:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.