### Question:
StaticResource { public int getResourceId(Context context) { int resourceId = context.getResources().getIdentifier( resourceName, resourceType, hasResourcePackage() ? resourcePackage : context.getPackageName()); checkResource(resourceId); return resourceId; } StaticResource(String value); int getResourceId(Context context); @Override String toString(); static final int RESOURCE_ID_NOT_EXIST; }### Answer:
@Test public void givenResourceNameTypeAndPackage_thenGetResourceIdFromContextResources() { MockResourcesBuilder aContextOfResources = aContextOfResources(); int expectedResourceId = aContextOfResources.declareResource(RESOURCE_NAME, RESOURCE_TYPE, RESOURCE_PACKAGE); StaticResource resource = new StaticResource(resourceValue(RESOURCE_NAME, RESOURCE_TYPE, RESOURCE_PACKAGE)); assertThat(resource.getResourceId(aContextOfResources.build()), equalTo(expectedResourceId)); }
@Test public void givenOnlyResourceNameAndType_thenUseContextPackageToGetResourceId() { MockResourcesBuilder aContextOfResources = aContextOfResources(); int expectedResourceId = aContextOfResources.withDefaultPackage(RESOURCE_PACKAGE).declareResource(RESOURCE_NAME, RESOURCE_TYPE, RESOURCE_PACKAGE); StaticResource resource = new StaticResource(resourceValue(RESOURCE_NAME, RESOURCE_TYPE)); assertThat(resource.getResourceId(aContextOfResources.build()), equalTo(expectedResourceId)); }
@Test(expected = RuntimeException.class) public void givenAResourceThatDoesNotExist_thenThrowRuntimeExceptionWhenGettingResourceId() { Resources resources = mock(Resources.class); when(resources.getIdentifier(anyString(), anyString(), anyString())).thenReturn(0); Context context = mock(Context.class); when(context.getResources()).thenReturn(resources); StaticResource resource = new StaticResource("@layout/non_existent_resource"); resource.getResourceId(context); }