Hamcrest Bookmarks

Published: 2019-08-04, Updated: 2023-03-19

Links

Teste Padrão de API

// act
final var response = given()
  .cookie(Cookies.GA_UNIQUE_USER_IDENTIFIER, "GA1.1.1236122116.1674662056")
  .get("/tools/live-viewers/live-settings.html")
  .then()
  .log()
  .ifValidationFails();

// assert
response
  .statusCode(Response.Status.OK.getStatusCode())
  .body(not(containsString("\"msg\"")))
  .body(containsString("www.twitch.tv/aliizzera23</textarea>"))
  .log()
;

Comparar objetos complexos junit

I realise this was asked a couple years ago, probably this feature wasn't around then. But now, it's easy to just do this:

import static org.hamcrest.MatcherAssert.assertThat;	
import static org.hamcrest.CoreMatchers.is;
@Test
public void test_array_pass()
{
  List<String> actual = Arrays.asList("fee", "fi", "foe");
  List<String> expected = Arrays.asList("fee", "fi", "foe");

  assertThat(actual, is(expected));
  assertThat(actual, is(not(expected)));
}

If you have a recent version of Junit installed with hamcrest, just add these imports:

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

http://junit-team.github.io/junit/javadoc/latest/org/junit/Assert.html#assertThat(T, org.hamcrest.Matcher)

http://junit-team.github.io/junit/javadoc/latest/org/hamcrest/CoreMatchers.html

http://junit-team.github.io/junit/javadoc/latest/org/hamcrest/core/Is.html

Verificar propriedade de objetos dentro de uma lista

validator.andExpect(status().isOk()).andExpect(model().attribute("reportItems", hasItem(
  allOf(
          hasProperty("id", is(1L))
  )
)));

Verificar propriedade de um objeto

validator.andExpect(model().attribute("form", hasProperty(
        "importDateInitial", is(new LocalDate().minusMonths(months).toDateTimeAtStartOfDay().toDate())
    )
));

Verificar atributos retornados no model da view com o spring mvc

validator = mockMvc.perform(get("/conciliation/equals/return.jhtml"));
validator.andDo(print());
validator.andExpect(status().isOk())
  .andExpect(model().attribute("reportItems", hasItems()));

Ver se duas listas tem os mesmos items de forma desorganizada

Assert.assertThat(
	Arrays.asList(),
	Matchers.containsInAnyOrder(Arrays.asList())
);

Se precisar validar a propriedade então implemente o equals

Validar se duas listas tem os seguintes items

List<MyObject> actual = Arrays.asList(new MyObject());
for(MyObject o: Arrays.asList(new MyObject())) 
	Assert.assertThat(
		list,
		CoreMatchers.hasItem(
			Matchers.samePropertyValuesAs(actual.get(i))
		)
	);

Se uma propriedade do objeto json tem qualquer um dos valores apontados

assertThat(
  jsonPath(config).getString("dockerHost"),
  CoreMatchers.anyOf(CoreMatchers.containsString("unix:"), CoreMatchers.containsString("npipe"))
);


Diminuir espaçamento da interface grafica do eclipse Cheat Engine Commands

Comments