Skip to content

Support using identifiers generated by database as method arguments#2285

Merged
EgorkaKulikov merged 3 commits into
mainfrom
ilya_m/generated_id_as_argument
Jun 15, 2023
Merged

Support using identifiers generated by database as method arguments#2285
EgorkaKulikov merged 3 commits into
mainfrom
ilya_m/generated_id_as_argument

Conversation

@IlyaMuravjov

@IlyaMuravjov IlyaMuravjov commented Jun 14, 2023

Copy link
Copy Markdown
Collaborator

Description

For reproduceable tests for methods that can read database and accept identifiers, we want to first fill up the database, take some identifier generated by the database and then pass that identifier to the method under test. That also solves the problem of fuzzer not always being able to guess identifiers that are used by the database, for example, when database uses random UUIDs.

How to test

Manual tests

Generate tests for OrderService#deleteOrderById(Long) from spring-boot-testing-main project, there should be test that looks like this:

@Test
@DisplayName("deleteOrderById: id = orderRepository.save(id = Order()).id -> return true")
public void testDeleteOrderByIdReturnsTrue() throws ClassNotFoundException, IllegalAccessException, NoSuchFieldException, InvocationTargetException, NoSuchMethodException {
    OrderService orderService = ((OrderService) applicationContext.getBean("orderService"));
    OrderRepository orderRepository = ((OrderRepository) applicationContext.getBean("orderRepository"));
    Order order = new Order();
    orderRepository.save(order);
    OrderRepository orderRepository1 = ((OrderRepository) applicationContext.getBean("orderRepository"));
    Order order1 = new Order();
    order1.setQty(1);
    order1.setBuyer("");
    order1.setPrice(Double.NEGATIVE_INFINITY);
    order1.setId(0L);
    Order order2 = ((Order) orderRepository1.save(order1));
    Long id = ((Long) getFieldValue(order2, "com.rest.order.models.Order", "id"));

    boolean actual = orderService.deleteOrderById(id);

    assertTrue(actual);
}

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 added ctg-enhancement New feature, improvement or change request comp-spring Issue is related to Spring projects support labels Jun 14, 2023
@IlyaMuravjov IlyaMuravjov requested a review from Markoutte June 14, 2023 08:34
@IlyaMuravjov IlyaMuravjov added the comp-fuzzing Issue is related to the fuzzing label Jun 14, 2023
@IlyaMuravjov IlyaMuravjov marked this pull request as ready for review June 14, 2023 16:31
@EgorkaKulikov EgorkaKulikov merged commit d7319b0 into main Jun 15, 2023
@EgorkaKulikov EgorkaKulikov deleted the ilya_m/generated_id_as_argument branch June 15, 2023 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-fuzzing Issue is related to the fuzzing comp-spring Issue is related to Spring projects support ctg-enhancement New feature, improvement or change request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants