Java SDK: Mockito.Spy doesn't work on activities

No response needed. Just noting here for completeness.

When constructing unit-tests with JUnit, it’s useful to use Mockito.Spy, e.g. to track call counts into activity methods. However, in constructing Spy instances, I’ve noticed that Spy strips the @ActivityInterface annotations, thus causing a Temporal runtime error attempting to use the activity.

What version of mockito are you using? Tried with 3.11.2
and it seems not stripped, you can test with for example:

MyActivities act = spy(MyActivities.class);
and then see that
act.getClass().getAnnotation(ActivityInterface.class)
is indeed present

I’ve tried with 3.12.4 and 3.11.2, but it won’t work.

In the sample above, you’re using the interface. In my JUnit test, I create an instance of the activity impl, and wrap that with the spy. Then add the impl to the worker. So I’m wondering if your sample isn’t valid in this case. Whatever Spy is doing to insert hooks, it’s not re-attaching the annotations.

Also, your example is fetching the annotation from the interface. If you’re on the impl class, you’d have to navigate up the class hierarchy to find the annotation, e.g.

   backupSetActivityImpl.getClass().getInterfaces()[0].getAnnotation(ActivityInterface.class)

So I stick with the original statement, Spy is stripping the annotations in it’s hackery.

I’ll also state the obvious: not a Temporal problem! :slight_smile:

Yep :slight_smile:
https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Spy.html

“Note that the spy won’t have any annotations of the spied type, because CGLIB won’t rewrite them. It may troublesome for code that rely on the spy to have these annotations.”