In my application I have quite a few service classes that act as a façade and delegate most calls to one or more underlying manager classes. I've read very different opinions on how to test such façades. Some people say it's not necessary to test them at all, others say one should test whether the integration is working.
I've implemented two test methods for the delegating method below. One test is checking whether the underlying manager method has been called exactly once, the other test checks whether the passed data is equal (or in this case: the same).
@Stateless public class ApplicationService { @Inject ApplicationManager applicationManager; public List<Application> getApplicationsList() { return applicationManager.getApplications(); } ... }
@RunWith(CdiRunner.class)
public class ApplicationServiceTest {
@Produces
@ProducesAlternative
@Mock
ApplicationManager manager;
@Inject
ApplicationService service;
@Test
public void testGetApplicationsListCalled() {
service.getApplicationsList();
verify(manager, times(1)).getApplications();
}
@Test
public void testGetApplicationsListPassthrough() {
ArrayList<Application> given = new ArrayList<>();
given.add(new Application("app1", "desc1", BusinessCriticality.C1));
given.add(new Application("app2", "desc2", BusinessCriticality.C1));
when(manager.getApplications()).thenReturn(given);
List<Application> resulting = service.getApplicationsList();
Assert.assertEquals(given, resulting);
Assert.assertEquals(2, resulting.size());
}
}
I'd love to hear opinions from experienced devs on whether testing such methods makes sense and if it does, which method of testing is better.