Skip to content

Improve the readability of integration test generated for Spring controller #2562

Closed
@EgorkaKulikov

Description

@EgorkaKulikov

Description

Consider generating Spring integration tests for the following controller method

@RestController
@RequestMapping(value = "/api")
public class OrderController {

    private OrderService orderService;

    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    @PostMapping(path = "/isMajorityExpensive")
    public boolean isMajorityExpensive(@RequestParam int threshold) {
        return orderService.isMajorityExpensive(threshold);
    }
}

where service contains the function

public boolean isMajorityExpensive(int threshold)

Expected behavior

Generated code looks like as follows:

    @Test
    public void testIsMajorityExpensive() throws Exception {
        UriComponentsBuilder uriComponentsBuilder = fromPath("/api/isMajorityExpensive");
        Object[] values = new Object[] { 4097 };
        UriComponentsBuilder uriComponentsBuilder1 = uriComponentsBuilder.queryParam("threshold", values);
        String urlTemplate = uriComponentsBuilder1.toUriString();
        Object[] uriVariables = {};
        MockHttpServletRequestBuilder mockHttpServletRequestBuilder = post(urlTemplate, uriVariables);

        ResultActions actual = mockMvc.perform(mockHttpServletRequestBuilder);

        actual.andDo(print());
        actual.andExpect((status()).is(200));
        actual.andExpect((content()).string("false"));
    }

Context

Current appearance is worse:

    @Test
    public void testIsMajorityExpensive() throws Exception {
        UriComponentsBuilder uriComponentsBuilder = fromPath("/api/isMajorityExpensive");
        List list = new ArrayList();
        list.add(4097);
        UriComponentsBuilder uriComponentsBuilder1 = uriComponentsBuilder.queryParam("threshold", ((Collection) list));
        String string = uriComponentsBuilder1.toUriString();
        Object[] objectArray = {};
        MockHttpServletRequestBuilder mockHttpServletRequestBuilder = post(string, objectArray);

        ResultActions actual = mockMvc.perform(mockHttpServletRequestBuilder);

        actual.andDo(print());
        actual.andExpect((status()).is(200));
        actual.andExpect((content()).string("false"));
    }

Metadata

Metadata

Assignees

Labels

comp-codegenIssue is related to code generatorcomp-springIssue is related to Spring projects supportctg-enhancementNew feature, improvement or change request

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions