Skip to main content
Tweeted twitter.com/StackSoftEng/status/794007424122449920
added 3 characters in body; edited title
Source Link
Tulains Córdova
  • 39.5k
  • 13
  • 99
  • 155

Whether and how to test facadesfaçades

In my application I have quite a few service classes that act as a facadefaçade and delegate most calls to one or more underlying manager classes. I've read very different opinions on how to test such facadesfaç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.

Whether and how to test facades

In my application I have quite a few service classes that act as a facade and delegate most calls to one or more underlying manager classes. I've read very different opinions on how to test such facades. 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 whether testing such methods makes sense and if it does, which method of testing is better.

Whether and how to test façades

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.

Post Migrated Here from codereview.stackexchange.com (revisions)
Source Link
Hugo G
  • 270
  • 4
  • 9

Whether and how to test facades

In my application I have quite a few service classes that act as a facade and delegate most calls to one or more underlying manager classes. I've read very different opinions on how to test such facades. 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 whether testing such methods makes sense and if it does, which method of testing is better.