Elena
Elena

Reputation: 501

Mockito with transactional annotated method

We've got a class with a method with an transactional annotation

public class MyHandler extends BaseHandler {

    @Transactional
    public void annotatedMethod(String parameter) throws Exception {
    }

As soon as we try to make a test using mockito and its annotations

@InjectMocks
private Controller controller;

@Mock
private MyHandler myHandler;

we have a null pointer exception when executing the following line

Mockito.doCallRealMethod().when(myHandler).annotatedMethod(Matchers.any());

We have tried to use the same solution as in Transactional annotation avoids services being mocked with no luck.

We're new with mockito, we have no idea how to solve this. Any clues?

Thank you very much, Elena

Upvotes: 0

Views: 1354

Answers (1)

infD
infD

Reputation: 63

you can try this way to mock the data. I have the similar NullPointerException and this way helped me.

@InjectMocks
private Controller controller;

@Mock
private MyHandler myHandler;

@Test
void testingMethod() {
    when(myHandler.annotatedMethod(Mockito.any())).thenReturn("something");
}

Upvotes: 1

Related Questions