Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
EgorkaKulikov opened this issue Aug 28, 2023 · 0 comments · Fixed by #2564
Closed

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

EgorkaKulikov opened this issue Aug 28, 2023 · 0 comments · Fixed by #2564
Assignees
Labels
comp-codegen Issue is related to code generator comp-spring Issue is related to Spring projects support ctg-enhancement New feature, improvement or change request

Comments

@EgorkaKulikov
Copy link
Collaborator

EgorkaKulikov commented Aug 28, 2023

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"));
    }
@EgorkaKulikov EgorkaKulikov added ctg-enhancement New feature, improvement or change request comp-codegen Issue is related to code generator comp-spring Issue is related to Spring projects support labels Aug 28, 2023
@EgorkaKulikov EgorkaKulikov changed the title Improve the readability Improve the readability of integration test generated for Spring controller Aug 28, 2023
@github-project-automation github-project-automation bot moved this from Todo to Done in UTBot Java Aug 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp-codegen Issue is related to code generator comp-spring Issue is related to Spring projects support ctg-enhancement New feature, improvement or change request
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants