Mocking methods in action test

Hi Team,

How to mock the method which is in the same class we are testing?

PFB the sample test case which I’m trying in my project:

class SampleAction { (generated from stateless action)

public handleEvent() {

registry();
}

public registry() {

}
}

Test case

@Test
void handleEventTest() {
final SampleAction mockA = Mockito.mock(SampleAction .class);
final MockRegistry mockService = MockRegistry.create().withMock(SampleAction .class, mockA);

when(mockA .registry()).thenReturn(“test”));

final SampleActionTestKit testKit = SampleActionATestKit.of(SampleAction::new, mockService);
final ActionResult result = testKit.handleEvent();

}

When I try this, it is always going inside the actual registry method, instead of mocked one.

Can someone please help to mock the method which is present inside the same class where the testing method present

Hey Thamizh07,

Indeed, the MockRegistry is meant to mock inter-component calls and not calls within the same component.

Given your request to mock registry() I assume such method might have some non-trivial logic or even external dependency. Could it make sense to extract that to an external dependency injected on the Action constructor? This would allow you to inject a dummy/mock object when in tests easily.