Skip to content

PowerMock tests are causing ClassCastException in assertJ best practices receipt #785

@Tutorialwork

Description

@Tutorialwork

What version of OpenRewrite are you using?

I am using

  • Maven/Gradle plugin v6.15.0
  • rewrite-testing-frameworks v3.14.1

How are you running OpenRewrite?

I am using the Maven plugin, and my project is a big project with multiple modules. That's why I excluded folders to speed up the proccess.

<plugin>
      <groupId>org.openrewrite.maven</groupId>
      <artifactId>rewrite-maven-plugin</artifactId>
      <version>6.15.0</version>
      <configuration>
        <exportDatatables>true</exportDatatables>
        <activeRecipes>
          <recipe>org.openrewrite.java.testing.assertj.Assertj</recipe>
        </activeRecipes>
        <exclusions>
          <exclusion>services/*</exclusion>
          <exclusion>services/**</exclusion>
          <exclusion>*/services/**</exclusion>
          <exclusion>*/log/**</exclusion>
          <exclusion>*/target/**</exclusion>
          <exclusion>*/datascience/**</exclusion>
        </exclusions>
      </configuration>
      <dependencies>
        <dependency>
          <groupId>org.openrewrite.recipe</groupId>
          <artifactId>rewrite-testing-frameworks</artifactId>
          <version>3.14.1</version>
        </dependency>
      </dependencies>
    </plugin>

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

I think the setUp method is triggering the issue but I'm not sure.

@Before
  public void setUp() throws Exception {
    PowerMockito.mockStatic(System.class);
    PowerMockito.when(System.currentTimeMillis()).thenReturn(CURRENT_TIME_MILLIS);

    PowerMockito.whenNew(Disruptor.class).withAnyArguments().thenReturn(this.disruptorMock);

    PowerMockito.doAnswer(
            invocation -> {
              this.eventHandler = invocation.getArgument(0);
              return Mockito.mock(EventHandlerGroup.class);
            })
        .when(this.disruptorMock)
        .handleEventsWith(ArgumentMatchers.<EventHandler<? super Event>>any());

    PowerMockito.when(this.disruptorMock.getRingBuffer()).thenReturn(this.ringBufferMock);

    PowerMockito.when(this.ringBufferMock.tryPublishEvent(Mockito.any()))
        .thenAnswer(
            invocation -> {
              EventTranslator<Event> translator = invocation.getArgument(0);
              translator.translateTo(this.eventMock, 0);
              return true;
            });

    PowerMockito.whenNew(CrossdeviceService.Client.class)
        .withAnyArguments()
        .thenReturn(this.crossDeviceServiceMock);

    PowerMockito.whenNew(TSocket.class)
        .withArguments(HOST, THRIFT_PORT)
        .thenReturn(this.tSocketMock, this.monitoringSocketMock);

    this.eventMock = Mockito.spy(new Event());

    PowerMockito.when(this.cdoEventMock.hasUserId()).thenReturn(true);
    PowerMockito.when(this.cdoEventMock.getUserIdValue()).thenReturn("1");

    PowerMockito.when(this.cdoEventMock.hasDomain()).thenReturn(true);
    PowerMockito.when(this.cdoEventMock.getDomain()).thenReturn(DOMAIN);

    PowerMockito.when(this.cdoEventMock.hasUrl()).thenReturn(true);
    PowerMockito.when(this.cdoEventMock.getUrl()).thenReturn(URL);

    PowerMockito.when(this.cdoEventMock.hasSourceInfo()).thenReturn(true);
    PowerMockito.when(this.cdoEventMock.getSourceInfo()).thenReturn(SOURCE_INFO);

    PowerMockito.when(this.cdoEventMock.hasIp()).thenReturn(true);
    PowerMockito.when(this.cdoEventMock.getIp())
        .thenReturn(new IpAddress("192.168.1.10", "2001:db8:85a3:370:7334:8a2e:370:7334"));

    PowerMockito.when(this.cdoEventMock.hasConsentString()).thenReturn(true);
    PowerMockito.when(this.cdoEventMock.getConsentString()).thenReturn(CONSENT_STRING);

    PowerMockito.when(this.cdoEventMock.hasSourceType()).thenReturn(true);
    PowerMockito.when(this.cdoEventMock.getSourceType()).thenReturn(SOURCE_TYPE);

    this.crossDeviceServer = new CrossDeviceServer(HOST, this.monitoringMock, SALT);
  }

What did you expect to see?

No errors.

What did you see instead?

A stacktrace

What is the full stack trace of any errors you encountered?

I'm running the plugin with the following command:

MAVEN_OPTS="-Xmx32g -Xms11g" mvn rewrite:run -pl :cdo-forwarder -am -T24C

Resulting in this stacktrace.

[ERROR] Failed to execute goal org.openrewrite.maven:rewrite-maven-plugin:6.15.0:run (default-cli) on project cdo-forwarder: Execution default-cli of goal org.openrewrite.maven:rewrite-maven-plugin:6.15.0:run failed: Error while visiting common/cdo-forwarder/src/test/java/com/activeagent/crossdevice/forwarder/CrossDeviceServerTest.java: java.lang.ClassCastException: class org.openrewrite.java.tree.J$FieldAccess cannot be cast to class org.openrewrite.java.tree.J$Identifier (org.openrewrite.java.tree.J$FieldAccess and org.openrewrite.java.tree.J$Identifier are in unnamed module of loader org.codehaus.plexus.classworlds.realm.ClassRealm @2fe033d7)
[ERROR]   org.openrewrite.java.testing.mockito.PowerMockitoWhenNewToMockito$1.lambda$removeMockUsagesVisitor$1(PowerMockitoWhenNewToMockito.java:113)
[ERROR]   java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
[ERROR]   java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1708)
[ERROR]   java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
[ERROR]   java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
[ERROR]   java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921)
[ERROR]   java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
[ERROR]   java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682)
[ERROR]   org.openrewrite.java.testing.mockito.PowerMockitoWhenNewToMockito$1.removeMockUsagesVisitor(PowerMockitoWhenNewToMockito.java:113)
[ERROR]   org.openrewrite.java.testing.mockito.PowerMockitoWhenNewToMockito$1.visitMethodDeclaration(PowerMockitoWhenNewToMockito.java:87)
[ERROR]   org.openrewrite.java.testing.mockito.PowerMockitoWhenNewToMockito$1.visitMethodDeclaration(PowerMockitoWhenNewToMockito.java:57)
[ERROR]   org.openrewrite.java.tree.J$MethodDeclaration.acceptJava(J.java:4036)
[ERROR]   org.openrewrite.java.tree.J.accept(J.java:60)
[ERROR]   org.openrewrite.TreeVisitor.visit(TreeVisitor.java:245)
[ERROR]   org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:311)
[ERROR]   org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1374)
[ERROR]   ...
[ERROR] -> [Help 1]

Are you interested in contributing a fix to OpenRewrite?

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions