Estoy queriendo usar dos emparejamientos personalizados para un solo método. Básicamente, si paso el método VALOR_A, quiero que devuelva RESULT_A, y si lo paso a VALUE_B, quiero que devuelva RESULT_B. Así que aquí está un extracto de código:Mockito actúa de manera extraña cuando asigno múltiples emparejamientos personalizados a un solo método
class IsNonEmpty extends ArgumentMatcher<Get> {
public boolean matches(Object get) {
//For some reason, this method is called when I assign the IsEmpty matcher to MockHtable.get()
//When this happens, the value of the get argument is null, so this method throws an NPE
return Arrays.equals(((Get) get).getRow(), SERIALIZATION_HELPER.getValidBytes(key));
}
}
class IsEmpty extends ArgumentMatcher<Get> {
public boolean matches(Object get) {
return !(Arrays.equals(((Get) get).getRow(), SERIALIZATION_HELPER.getValidBytes(key)));
}
}
[...]
//This line executes just fine
Mockito.when(mockHTable.get(Mockito.argThat(new IsNonEmpty()))).thenReturn(dbResult);
[...]
//This line calls IsNonEmpty.matches() for some reason. IsNonEmpty.matches() throws an NPE
Mockito.when(mockHTable.get(Mockito.argThat(new IsEmpty()))).thenReturn(emptyResult);
Cuando le asigno el encargo de coincidencias IsEmpty al método mockHTable.get(), llama a la función IsNonEmpty.matches(). No tengo idea de por qué está haciendo esto. Así que cambio la clase IsNonEmpty a esto:
class IsNonEmpty extends ArgumentMatcher<Get> {
public boolean matches(Object get) {
//For some reason, this method is called when I assign the IsEmpty matcher. Weird, no?
if(get == null) {
return false;
}
return Arrays.equals(((Get) get).getRow(), SERIALIZATION_HELPER.getValidBytes(key));
}
}
y luego todo funciona bien! IsNonEmpty.matches() todavía se invoca cuando asigno el emparejador IsEmpty a la función mockHTable.get(), pero mis emparejamientos funcionan exactamente como deberían.
¿Cuál es el problema? ¿Por qué pasó esto? ¿Es mi forma de trabajo una forma adecuada de compensar este comportamiento peculiar, o lo estoy haciendo mal?