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

Use controllers via MockMvc in Spring integration tests #2447

Merged
merged 19 commits into from
Aug 1, 2023

Conversation

IlyaMuravjov
Copy link
Collaborator

@IlyaMuravjov IlyaMuravjov commented Jul 25, 2023

Description

Fixes #2345
Also fixes #2227

This PR makes integration tests for Spring controllers instead of calling controllers directly, call them via MockMvc.perform() and use andExpect() to do asserts.

In order to achieve that:

  • methodUnderTest and methodToCall were separated (methodToCall can be set in EnvironmentModels)
  • ConcreteExecutionContext was allowed to modify stateBefore (including methodToCall)
  • UtCustomModel was introduced to describe models with additional custom data (it has origin that can be used in common UtBot code base)
  • InstrumentationContext was enriched to provide UtCustomModelConstructors (including previously existing subclasses of UtAssembleModelConstructorBase)
  • CgSimpleCustomAssertConstructor was introduced to generate more human readable assertions when expectedResult is UtCustomModel (makes Avoid generating many reflection statements for LinkedHashMap #2227 no longer relevant for Spring controllers)

Example:

@SpringBootTest
@BootstrapWith(SpringBootTestContextBootstrapper.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
@Transactional
@AutoConfigureTestDatabase
@AutoConfigureMockMvc
public final class OrderControllerTest {
    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private ObjectMapper jacksonObjectMapper;

    @Test
    @DisplayName("saveOrder: order = Order()")
    public void testSaveOrder1() throws Exception {
        Object[] objectArray = {};
        MockHttpServletRequestBuilder mockHttpServletRequestBuilder = post("/api/orders", objectArray);
        MediaType mediaType = valueOf("application/json");
        MockHttpServletRequestBuilder mockHttpServletRequestBuilder1 = mockHttpServletRequestBuilder.contentType(mediaType);
        Order order = new Order();
        String string = jacksonObjectMapper.writeValueAsString(order);
        MockHttpServletRequestBuilder mockHttpServletRequestBuilder2 = mockHttpServletRequestBuilder1.content(string);

        ResultActions actual = mockMvc.perform(mockHttpServletRequestBuilder2);

        actual.andDo(print());
        actual.andExpect((status()).is(201));
        actual.andExpect((content()).string("{\"id\":28,\"buyer\":null,\"price\":null,\"qty\":0}"));
    }
}

How to test

Manual tests

Generate integration tests for OrderController in spring-boot-testing project, there should be tests that use MockMvc.

Self-check list

  • I've set the proper labels for my PR (at least, for category and component).
  • PR title and description are clear and intelligible.
  • I've added enough comments to my code, particularly in hard-to-understand areas.
  • The functionality I've repaired, changed or added is covered with automated tests.
  • Manual tests have been provided optionally.
  • The documentation for the functionality I've been working on is up-to-date.

@IlyaMuravjov IlyaMuravjov force-pushed the ilya_m/mock_mvc branch 3 times, most recently from 3996ba1 to e26100f Compare July 28, 2023 11:52
@IlyaMuravjov IlyaMuravjov added comp-spring Issue is related to Spring projects support ctg-enhancement New feature, improvement or change request comp-codegen Issue is related to code generator comp-instrumented-process Issue is related to Instrumented process labels Jul 28, 2023
@IlyaMuravjov IlyaMuravjov marked this pull request as ready for review July 28, 2023 12:38
@EgorkaKulikov EgorkaKulikov linked an issue Jul 31, 2023 that may be closed by this pull request
IlyaMuravjov added a commit that referenced this pull request Jul 31, 2023
IlyaMuravjov added a commit that referenced this pull request Jul 31, 2023
@EgorkaKulikov EgorkaKulikov merged commit c21a916 into main Aug 1, 2023
37 checks passed
@EgorkaKulikov EgorkaKulikov deleted the ilya_m/mock_mvc branch August 1, 2023 06:41
@alisevych alisevych added this to the Spring Phase 4 milestone Aug 11, 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-instrumented-process Issue is related to Instrumented process comp-spring Issue is related to Spring projects support ctg-enhancement New feature, improvement or change request
Projects
None yet
3 participants