1
resposta

caso de test na classe AvaliadorTest para testar os 3 menores casos utilizando Hamcrest

Prezado, Gostaria de saber como poderia nessa classe AvaliadorTest, implementar o test dos 3 menores valores, meu test com a biblioteca do JUnit funcionava, nao sei se era o certo, mas quando fui migrar para test com a biblioteca Hamcrest, deu o seguinte mensagem de erro:

"java.lang.AssertionError:  
Expected: (a collection containing <br.com.caelum.leilao.dominio.Lance@c96af00a> and a collection containing <br.com.caelum.leilao.dominio.Lance@44b9f22d> and a collection containing <br.com.caelum.leilao.dominio.Lance@c95af00a> and a collection containing <br.com.caelum.leilao.dominio.Lance@44a0322d>)
     but: a collection containing <br.com.caelum.leilao.dominio.Lance@c96af00a> was <br.com.caelum.leilao.dominio.Lance@44a0322d>, was <br.com.caelum.leilao.dominio.Lance@c95af00a>, was <br.com.caelum.leilao.dominio.Lance@44b9f22d>
    at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
    at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)
    at br.com.caelum.leilao.servico.AvaliadorTest.deveEncontrarOsTresMenoresLances(AvaliadorTest.java:241)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    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.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    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.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
"
// ---------------------------------------------------------------------------------------------------
// Esse e meu metodo de test
@Test
    public void deveEncontrarOsTresMenoresLances(){

        Leilao leilao = new CriadorDeLeilao()
                            .para("Playstation 3 Novo")
                            .lance(joao, 100.0)
                            .lance(maria, 200.0)
                            .lance(joao, 300.0)
                            .lance(maria, 400.0)
                            .constroi();

        leiloeiro.avalia(leilao);

        List<Lance> menores = leiloeiro.getTresMenores();
        assertEquals(3, menores.size());

        // teste com biblioteca JUnit
        // assertEquals(300.0, menores.get(2).getValor(), 0.00001);
        // assertEquals(200.0, menores.get(1).getValor(), 0.00001);
        // assertEquals(100.0, menores.get(0).getValor(), 0.00001);

        // teste com biblioteca Hamcrest
        assertThat(menores, hasItems(
                    new Lance(joao, 100.0),
                    new Lance(maria, 200.0),
                    new Lance(joao, 300.0),
                    new Lance(maria, 400.0)
                ));

    }
1 resposta

Essa classe Lance está com o equals implementado?