Skip to content

selectCustomerById Test #8

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

Open
hazartilirot opened this issue Apr 23, 2023 · 0 comments
Open

selectCustomerById Test #8

hazartilirot opened this issue Apr 23, 2023 · 0 comments

Comments

@hazartilirot
Copy link

That's really strange! We've got a block @beforeeach setup.... why don't we place the piece of code into it?

var customer = new Customer(
                FAKER.name().fullName(),
                FAKER.internet().safeEmailAddress(),
                new Random().nextInt(18, 99)
        );

customerJDBCDataAccessServiceUnitTest.insertCustomer(customer);

Why does he use the email address to find the customer we've just inserted? He basically relies on a method he hasn't tested yet. It's a shame!

If the previous test is passed we mainly need to rely on it instead.

    @Test
    void selectCustomerById() {
        var customer1 = customerJDBCDataAccessServiceUnitTest.selectAllCustomers()
                                                             .stream()
                                                             .findAny()
                                                             .orElseThrow();
        var customer2 = customerJDBCDataAccessServiceUnitTest.selectCustomerById(customer1.getId())
                                                             .orElseThrow();
        assertThat(customer1.getId()).isEqualTo(customer2.getId());

    }

We don't care what email our inserted user has.... All we care about is if there is a user at all. We get the user and then we test our method looking the user by its ID. Here is the code:

@BeforeEach
    void setUp() {
        customerJDBCDataAccessServiceUnitTest = new CustomerJDBCDataAccessService(getJdbcTemplate());

        var customer = new Customer(
                FAKER.name().fullName(),
                FAKER.internet().safeEmailAddress(),
                new Random().nextInt(18, 99)
        );

        customerJDBCDataAccessServiceUnitTest.insertCustomer(customer);
    }

    @Test
    void selectAllCustomers() {

        var customers = customerJDBCDataAccessServiceUnitTest.selectAllCustomers();

        assertThat(customers).isNotEmpty();
    }

    @Test
    void selectCustomerById() {
        var customer1 = customerJDBCDataAccessServiceUnitTest.selectAllCustomers()
                                                             .stream()
                                                             .findAny()
                                                             .orElseThrow();
        var customer2 = customerJDBCDataAccessServiceUnitTest.selectCustomerById(customer1.getId())
                                                             .orElseThrow();
        assertThat(customer1.getId()).isEqualTo(customer2.getId());

    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant