db-util icon indicating copy to clipboard operation
db-util copied to clipboard

Create a JUnit 5 extension to for clearer tests

Open dpash opened this issue 5 years ago • 1 comments

Currently, we need to write tests like:

@Test 
public void test() {
    SQLStatementCountValidator.reset();
    warehouseProductInfoService.findAllWithNPlusOne();
    assertSelectCount(1);
}

It would be useful if we could write

@ExtendWith(QueryCountExtension.class)
public class Tests {

    @QueryCount(select = 1)
    public void test() {
        warehouseProductInfoService.findAllWithNPlusOne();
    }
}

I can have a look at creating a pull request, but I wanted to get your feedback on whether this is something you'd be interested in and how you'd like the @QueryCount annotation to look like. I was thinking of having something like (with defaults): @QueryCount(select = 0, insert = 0, update = 0, delete = 0, total = <sum of others>)

I'd also suggest a number of methods that take a lambda:

assertSelectCount(1, () -> warehouseProductInfoService.findAllWithNPlusOne());

so that you can do multiple assertions in the same test.

dpash avatar Nov 04 '19 23:11 dpash

Looks like a good idea. Send me a Pull Request with a proposal for this.

vladmihalcea avatar Nov 05 '19 02:11 vladmihalcea

The project will be archived soon.

vladmihalcea avatar Dec 27 '22 16:12 vladmihalcea