volodya-lombrozo / jtcop

Maven Plugin for checking tests in Java projects
MIT License
18 stars 2 forks source link

Detect "Mockery" anti-pattern #393

Closed h1alexbel closed 2 months ago

h1alexbel commented 2 months ago

How about to detect Mockery anti-pattern?

Mockery definition: Sometimes mocking can be good, and handy. But sometimes developers can lose themselves in their effort to mock out what isn’t being tested. In this case, a unit test contains so many mocks, stubs, and/or fakes that the system under test isn’t even being tested at all, instead data returned from mocks is what is being tested.

Example:

import org.mockito.Mockito;

final class FooTest {

  @Test
  void testsSomething() {
    Frame frame = Mockito.mock(Frame.class);
    Mockito.doReturn(...).when(frame).iterator();
    Table table = Mockito.mock(Table.class);
    Mockito.doReturn(frame).when(table).frame();
    Region region = Mockito.mock(Region.class);
    Mockito.doReturn(table).when(region).table(Mockito.anyString());
 }
}

in pom.xml we can introduce new parameter maxNumberOfMocks:

<configuration>
  <maxNumberOfMocks>2</maxNumberOfMocks>
</configuration>

Thus, using example above, jtcop should fail with message like this:

[ERROR]: FooTest.java#testsSomething has too many mocks: 3 (max allowed 2)
h1alexbel commented 2 months ago

@volodya-lombrozo what do you think?

volodya-lombrozo commented 2 months ago

@h1alexbel I like this idea! We can implement it. However, it's better to check if somebody already implemented something similar inside other linters. Have you checked?

h1alexbel commented 2 months ago

@volodya-lombrozo not yet, but I can take research this topic

h1alexbel commented 2 months ago

@volodya-lombrozo assign me, please

volodya-lombrozo commented 2 months ago

@h1alexbel Thank you!

0pdd commented 2 months ago

@h1alexbel 3 puzzles #396, #397, #398 are still not solved.

0pdd commented 2 months ago

@h1alexbel 2 puzzles #396, #397 are still not solved; solved: #398.

0pdd commented 1 month ago

@h1alexbel the puzzle #397 is still not solved; solved: #396, #398.

volodya-lombrozo commented 1 month ago

@rultor release, tag is 1.3.0, title is 1.3.0.

rultor commented 1 month ago

@rultor release, tag is 1.3.0, title is 1.3.0.

@volodya-lombrozo OK, I will release it now. Please check the progress here

rultor commented 1 month ago

@rultor release, tag is 1.3.0, title is 1.3.0.

@volodya-lombrozo Done! FYI, the full log is here (took me 6min)