# Test heartbeating in service called within an activity

**URL:** <https://community.temporal.io/t/test-heartbeating-in-service-called-within-an-activity/11678>\
**Category:** Community Support\
**Tags:** java-sdk, testing\
**Created:** [April 9, 2024, 8:00am UTC](https://community.temporal.io/t/test-heartbeating-in-service-called-within-an-activity/11678 "2024-04-09T08:00:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bilal\_Fares](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/bilal_fares/32/4546_2.png) [@Bilal\_Fares](https://community.temporal.io/u/Bilal_Fares)\
**Post date:** [April 9, 2024, 8:00am UTC](https://community.temporal.io/t/test-heartbeating-in-service-called-within-an-activity/11678/1 "2024-04-09T08:00:02Z")

</div>

Hi,  
I have an activity that calls a service to perform a certain functionality. Inside my service method, I have a loop in which the service is performing heartbeats.  
I am trying to test this service method and getting the following error:

> ActivityExecutionContext can be used only inside of activity implementation methods and in the same thread that invoked an activity.

I tried to mock the static method `Activity.getExecutionContext` to return a mocked `ActivityExecutionContext` but still stuck with the same error.

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [April 9, 2024, 8:23am UTC](https://community.temporal.io/t/test-heartbeating-in-service-called-within-an-activity/11678/2 "2024-04-09T08:23:51Z")

</div>

Use TestActivityEnvironment to test activities.

---

<div class="post-metadata">

**Author:** ![Bilal\_Fares](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/bilal_fares/32/4546_2.png) [@Bilal\_Fares](https://community.temporal.io/u/Bilal_Fares)\
**Post date:** [April 9, 2024, 8:27am UTC](https://community.temporal.io/t/test-heartbeating-in-service-called-within-an-activity/11678/3 "2024-04-09T08:27:28Z")

</div>

Hello @maxim thanks for you reply,  
How can I use `TestActivityEnvironment` here in this case. I already use it to test activities but in this case I am testing a service class that is being called within an activity, not the activity itself.

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [April 9, 2024, 9:01am UTC](https://community.temporal.io/t/test-heartbeating-in-service-called-within-an-activity/11678/4 "2024-04-09T09:01:43Z")

</div>

Create a fake test-only activity that implements the testing scenario.
