top | item 46771386

(no title)

anthonypasq96 | 1 month ago

youre verifying std lib function call counts in unit tests? lmao.

discuss

order

tayo42|1 month ago

You can do that with mocks if it's important that something is only called once, or likely there's some unintended side effect of calling it twice and tests woukd catch the bug

anthonypasq96|1 month ago

i know you could do it, im asking why on earth you would feel its vital to verify stream.filter() was called twice in a function

noitpmeder|1 month ago

You're not verifying the observable behavior of your application? lmao

shagie|1 month ago

How would you suggest tests around:

    void func() {
        printEvens(someCall().stream().filter(n -> n % 2 == 0).toList());
    }

    void printEvens(List<Integer> nums) {
        nums.stream().filter(n -> n % 2 == 0).forEach(n -> System.out.println(n));
    }
The first filter is redundant in this example. Duplicate code checkers are checking for exactly matching lines.

I am unaware of any linter or static analyzer that would flag this.

What's more, unit tests to test the code for printEvens (there exists one) pass because they're working properly... and the unit test that calls the calling function passes because it is working properly too.

Alternatively, write the failing test for this code.

quietbritishjim|1 month ago

A redundant filter() isn't observable (except in execution time).

You could pick it up if you were to explicitly track whether it's being called redundantly but it'd be very hard and by the time you'd thought of doing that you'd certainly have already manually checked the code for it.

anthonypasq96|1 month ago

what happened to not testing implementation details?