### Question:
DeclaringTypeMatcher extends ElementMatcher.Junction.AbstractBase<T> { public boolean matches(T target) { TypeDefinition declaringType = target.getDeclaringType(); return declaringType != null && matcher.matches(declaringType.asGenericType()); } DeclaringTypeMatcher(ElementMatcher<? super TypeDescription.Generic> matcher); boolean matches(T target); @Override String toString();  }### Answer:
@Test public void testMatch() throws Exception { when(declaredByType.getDeclaringType()).thenReturn(typeDescription); when(typeMatcher.matches(typeDescription)).thenReturn(true); assertThat(new DeclaringTypeMatcher<DeclaredByType>(typeMatcher).matches(declaredByType), is(true)); verify(typeMatcher).matches(typeDescription); verifyNoMoreInteractions(typeMatcher); verify(declaredByType).getDeclaringType(); verifyNoMoreInteractions(declaredByType); }
@Test public void testNoMatch() throws Exception { when(declaredByType.getDeclaringType()).thenReturn(typeDescription); when(typeMatcher.matches(typeDescription)).thenReturn(false); assertThat(new DeclaringTypeMatcher<DeclaredByType>(typeMatcher).matches(declaredByType), is(false)); verify(typeMatcher).matches(typeDescription); verifyNoMoreInteractions(typeMatcher); verify(declaredByType).getDeclaringType(); verifyNoMoreInteractions(declaredByType); }
@Test public void testNoMatchWhenNull() throws Exception { assertThat(new DeclaringTypeMatcher<DeclaredByType>(typeMatcher).matches(declaredByType), is(false)); verifyZeroInteractions(typeMatcher); verify(declaredByType).getDeclaringType(); verifyNoMoreInteractions(declaredByType); }