JUnit testing fails for `session.save()` with error "Wanted but not invoked:"
I am trying to create a unit test that checks whether `session.save();` has been called, in my service implementation below:
TagManagerServiceImpl.java
@8220494(service = TagManagerService.class, immediate = true)
public class TagManagerServiceImpl implements TagManagerService {
private static final Logger LOGGER = LoggerFactory.getLogger(TagManagerServiceImpl.class);
@3214626
private ResourceResolverFactory resolverFactory;
@3214626
private JcrTagManagerFactory jcrTagManagerFactory;
public void createTag(String tagPath, String tagTitle, String tagDescription) {
ResourceResolver resourceResolver = null;
try {
resourceResolver = ResolverUtil.newResolver(resolverFactory);
Session session = resourceResolver.adaptTo(Session.class);
TagManager tagManager = jcrTagManagerFactory.getTagManager(session);
if (session != null) {
tagManager.createTag(tagPath, tagTitle, tagDescription, true);
session.save(); //<========== want to test this
}
} catch (Exception e) {
LOGGER.error("Error while creating tag", e);
} finally {
if (resourceResolver != null && resourceResolver.isLive()) {
resourceResolver.close();
}
}
}
}
My JUnit test is as follow:
TagManagerServiceTest.java
@ExtendWith(MockitoExtension.class)
public class TagManagerServiceTest {
@InjectMocks
private TagManagerServiceImpl tagManagerService;
@Mock
private ResourceResolverFactory resolverFactory;
@Mock
private JcrTagManagerFactory jcrTagManagerFactory;
@Mock
private Session session;
@Mock
private TagManager tagManager;
@Mock
private ResourceResolver resourceResolver;
@BeforeEach
public void setUp() {
MockitoAnnotations.initMocks(this);
try {
when(ResolverUtil.newResolver(resolverFactory)).thenReturn(resourceResolver);
when(resourceResolver.adaptTo(Session.class)).thenReturn(session);
when(jcrTagManagerFactory.getTagManager(session)).thenReturn(tagManager);
} catch (Exception e) {
fail("Expected no exceptions, but got: " + e.getMessage());
}
}
@2785667
public void testCreateTag() {
String tagPath = "some/tagPath";
String tagTitle = "TagTitle";
String tagDescription = "TagDescription";
tagManagerService.createTag(tagPath, tagTitle, tagDescription);
try {
verify(tagManager, times(1)).createTag(tagPath, tagTitle, tagDescription, true);
System.out.println(mockingDetails(session).printInvocations()); // No interactions and stubbings found for mock: session
verify(session, times(1)).save(); // <========= testing session.save();
} catch (Exception e) {
fail("Expected no exceptions, but got: " + e.getMessage());
}
}
}
But when I run the test, I get the following error. The line 66 mentioned being `verify(session, times(1)).save();` in the above code.
Wanted but not invoked:
session.save();
-> at com.mysite.core.services.TagManagerServiceTest.testCreateTag(TagManagerServiceTest.java:66)
Actually, there were zero interactions with this mock.
As you can see in my test code, I troubleshot by placing `System.out.println(mockingDetails(session).printInvocations());` right before the test, and got the printed message "No interactions and stubbings found for mock: session".
How can I make sure that `session.save();` is property tested?
Thanks,