Skip to content

RemoveDoNothingForDefaultMocks removes stubbings with ArgumentCaptor side effects #986

@protocol7

Description

@protocol7

What version of OpenRewrite are you using?

  • rewrite-testing-frameworks: 3.35.2
  • rewrite-core: 8.81.1
  • rewrite-java: 8.81.1

What is the smallest, simplest way to reproduce the problem?

import static org.mockito.Mockito.doNothing;

import org.mockito.ArgumentCaptor;
import org.mockito.Mock;

class ExampleTest {
  @Mock Client client;
  ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);

  void setUp() {
    doNothing().when(client).send(captor.capture());
  }

  interface Client {
    void send(String message);
  }
}

Run org.openrewrite.java.testing.mockito.RemoveDoNothingForDefaultMocks.

What did you expect to see?

The stubbing should be preserved because captor.capture() has a side effect. Removing the stubbing changes test behavior.

What did you see instead?

The recipe removes the entire doNothing().when(...) stubbing:

class ExampleTest {
  @Mock Client client;
  ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);

  void setUp() {
  }

  interface Client {
    void send(String message);
  }
}

Any later use of captor.getValue() can fail because no value was captured.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions