168 votos

¿Cómo se puede afirmar que algo es nulo con Hamcrest?

¿Cómo podría assertThat algo es null?

por ejemplo

 assertThat(attr.getValue(), is(""));

Pero recibo un error diciendo que no puedo tener null en is(null).

294voto

Rohit Jain Puntos 90368

Puedes usar el método [IsNull.nullValue()](http://junit.sourceforge.net/javadoc/org/hamcrest/core/IsNull.html#nullValue()):

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

assertThat(attr.getValue(), is(nullValue()));

35voto

Chetya Puntos 185

¿Por qué no usar assertNull(objeto) / assertNotNull(objeto)?

17voto

Sajan Chandran Puntos 4482

Si quieres usar hamcrest, puedes hacerlo

import static org.hamcrest.Matchers.nullValue;

assertThat(attr.getValue(), is(nullValue()));

En Junit puedes hacer

import static junit.framework.Assert.assertNull;
assertNull(object);

11voto

blackpanther Puntos 3456

Utilice lo siguiente (de Hamcrest):

assertThat(attr.getValue(), is(nullValue()));

En Kotlin is está reservado por lo que debe usar:

assertThat(attr.getValue(), `is`(nullValue()));

Iteramos.com

Iteramos es una comunidad de desarrolladores que busca expandir el conocimiento de la programación mas allá del inglés.
Tenemos una gran cantidad de contenido, y también puedes hacer tus propias preguntas o resolver las de los demás.

Powered by:

X