### Question:
JCPermissionFactory implements PermissionFactory { @Override public Permission buildFromName(String name) { Permission permission = jtalksPermissionFactory.buildFromName(name); if (permission == null) { permission = pluginPermissionFactory.buildFromName(name); } if (permission == null) { permission = NullPermission.NULL_PERMISSION; } return permission; } JCPermissionFactory(JtalksPermissionFactory jtalksPermissionFactory, PluginsPermissionFactory pluginPermissionFactory); @Override Permission buildFromMask(int mask); @Override Permission buildFromName(String name); @Override List<Permission> buildFromNames(List<String> names);  }### Answer:
@Test public void buildFromNameShouldReturnPermissionFromCommons() { JtalksPermission targetPermission = BranchPermission.VIEW_TOPICS; when(jtalksPermissionFactory.buildFromName(targetPermission.getName())).thenReturn(targetPermission); JCPermissionFactory permissionFactory = new JCPermissionFactory(jtalksPermissionFactory,pluginsPermissionFactory); Permission result = permissionFactory.buildFromName(targetPermission.getName()); assertEquals(result, targetPermission); verify(pluginsPermissionFactory, never()).buildFromName(anyString()); }
@Test public void buildFromNameShouldReturnPluginPermissionWhenCommonNotFound() { JtalksPermission targetPermission = BranchPermission.VIEW_TOPICS; when(jtalksPermissionFactory.buildFromName(anyString())).thenReturn(null); when(pluginsPermissionFactory.buildFromName(targetPermission.getName())).thenReturn(targetPermission); JCPermissionFactory permissionFactory = new JCPermissionFactory(jtalksPermissionFactory,pluginsPermissionFactory); Permission result = permissionFactory.buildFromName(targetPermission.getName()); assertEquals(result, targetPermission); verify(jtalksPermissionFactory).buildFromName(targetPermission.getName()); }
@Test public void buildFromNameShouldReturnAnyNotNullDefaultValueWhenNoPermissionFound() { JtalksPermission targetPermission = BranchPermission.VIEW_TOPICS; when(jtalksPermissionFactory.buildFromName(anyString())).thenReturn(null); when(pluginsPermissionFactory.buildFromName(anyString())).thenReturn(null); JCPermissionFactory permissionFactory = new JCPermissionFactory(jtalksPermissionFactory,pluginsPermissionFactory); Permission result = permissionFactory.buildFromName(targetPermission.getName()); assertNotNull(result); verify(jtalksPermissionFactory).buildFromName(targetPermission.getName()); verify(pluginsPermissionFactory).buildFromName(targetPermission.getName()); }