Mockito matcher y matriz de primitivas
Con Mockito, quiero verify()
llamar a un método con byte[]
en su lista de argumentos, pero no encontré cómo escribir esto.
myMethod( byte[] )
Solo quiero algo como anyByteArray()
, ¿cómo hacer eso con Mockito ?
182
8 answers
Intentaría any(byte[].class)
349
Author: gpeche,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-08 21:42:26
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-08 21:42:26
Prueba esto:
AdditionalMatchers.aryEq(array);
23
Author: Alécio Carvalho,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-16 16:50:36
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-16 16:50:36
Prefiero usar Matchers.<byte[]>any()
. Esto funcionó para mí.
11
Author: Fabiano Francesconi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-01-29 13:42:32
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-01-29 13:42:32
Estoy de acuerdo con Mutanos y Alecio. Además, se pueden verificar tantas llamadas de método idénticas como sea posible (verificar las llamadas posteriores en el código de producción, el orden de la verificación no importa). Aquí está el código:
import static org.mockito.AdditionalMatchers.*;
verify(mockObject).myMethod(aryEq(new byte[] { 0 }));
verify(mockObject).myMethod(aryEq(new byte[] { 1, 2 }));
7
Author: Rene Ummels,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-25 09:53:09
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-25 09:53:09
O..
byte[] sampleTextFileAsBytes = new byte[]{anyByte()};
1
Author: kunalk83,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-12 02:29:57
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-12 02:29:57
Puedes usar Mockito.any() cuando los argumentos también son matrices. Lo usé así:
verify(myMock, times(0)).setContents(any(), any());
0
Author: Crenguta S,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-13 16:25:52
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-13 16:25:52
Usé Matchers.refEq
para esto.
0
Author: Bowofola,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-16 19:36:03
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-16 19:36:03
Siempre puede crear un Matcher personalizado usando argThat
Mockito.verify(yourMockHere).methodCallToBeVerifiedOnYourMockHere(ArgumentMatchers.argThat(new ArgumentMatcher<Object>() {
@Override
public boolean matches(Object argument) {
YourTypeHere[] yourArray = (YourTypeHere[]) argument;
// Do whatever you like, here is an example:
if (!yourArray[0].getStringValue().equals("first_arr_val")) {
return false;
}
return true;
}
}));
0
Author: Koray Tugay,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-10-02 14:42:42
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-10-02 14:42:42