Está ocorrendo erro ao usar o assertThat com o hasItems.
Usando os seguintes imports do hamcrest 1.3:
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.MatcherAssert.assertThat;
Usando o seguinte código assertThat no método de teste (últimas linhas):
@Test
public void deveEncontrarOsTresMaioresLances() {
// Parte 1: Cenário
Leilao leilao = new CriadorDeLeilao().para("Sony Playstation 4")
.lance(joao, 100.0)
.lance(maria, 200.0)
.lance(joao, 300.0)
.lance(maria, 400.0)
.constroi();
// Parte 2: Ação
leiloeiro.avalia(leilao);
List<Lance> maiores = leiloeiro.getTresMaiores();
// Parte 3: Avaliação de resultados
assertEquals(3, maiores.size());
assertThat(maiores, hasItems(
new Lance(maria, 400.0),
new Lance(joao, 300.0),
new Lance(maria, 200.0)
));
}
E usando o seguinte código para o método equals na classe Leilao:
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Leilao other = (Leilao) obj;
if (descricao == null) {
if (other.descricao != null)
return false;
} else if (!descricao.equals(other.descricao))
return false;
if (lances == null) {
if (other.lances != null)
return false;
} else if (!lances.equals(other.lances))
return false;
return true;
}
Mas ao executar os testes, está sendo exibido o seguinte erro:
java.lang.AssertionError:
Expected: (a collection containing <br.com.caelum.leilao.dominio.Lance@710726a3> and a collection containing <br.com.caelum.leilao.dominio.Lance@646007f4> and a collection containing <br.com.caelum.leilao.dominio.Lance@481a15ff>)
but: a collection containing <br.com.caelum.leilao.dominio.Lance@710726a3> was <br.com.caelum.leilao.dominio.Lance@78186a70>, was <br.com.caelum.leilao.dominio.Lance@306279ee>, was <br.com.caelum.leilao.dominio.Lance@545997b1>
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)
at br.com.caelum.leilao.servico.AvaliadorTest.deveEncontrarOsTresMaioresLances(AvaliadorTest.java:145)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Gostaria de saber o motivo erro.
Obrigado