function equals of object is not called during unit test

Asked

Viewed 189 times

1

I’m developing unit test of a class with Junit and Mockite, and to facilitate the creation of a spy of this class I created a function that builds and returns this spy. The problem that is occurring is that in the call flow that is performed to perform the test, when the method call is made equals of that Spy, instead of calling the function implemented in the class, is calling a function of the class MethodInterceptorFilter. Does anyone know how to get him to get the function implemented in the class?

test function:

@Test
public void requisicaoInvalidaNomeDuplicado() {
    prov.add(umProvTO("Premio", 5, new BenTO( 32, new BigDecimal(200) )));
    prov.add(umProvTO("Premio", 10, new BenTO( 32, new BigDecimal(500) )));

    assertEquals(false, requisicao.validarNomeDuplicado());
}

implementation of the function validarNomeDuplicado()

private List<ProvTO> provs;

public boolean validarNomeDuplicado() {
    for (ProvTO prov : provs) {
        if (Collections.frequency(provs, prov) > 1) {
            return false;
        }
    }
    return true;
}

class ProvTO:

private String nome;

[...]

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (!(obj instanceof ProvTO)) {
        return false;
    }
    ProvTO other = (ProvTO) obj;
    if (nome == null) {
        if (other.nome != null) {
            return false;
        }
    } else if (!nome.equals(other.nome)) {
        return false;
    }
    return true;
}
  • Can you validate if the published code snippet is correct? The No Increase function is accessing provs and not Prov. And there is a class umProvTO.java?

  • provs is of private List<ProvTO> provs;

1 answer

0

Apparently there’s a problem with the design of your test. Why create a spy of that class ProvTO? You create the spy, but in no time do I see you using the verify.

The spy is used to verify class interactions. It is used for a specific type of test. For example, imagine that in this method validarNomeDuplicado you called some other method:

public boolean validarNomeDuplicado() {
    procedimento();
    for (ProvTO prov : provs) {
        if (Collections.frequency(provs, prov) > 1) {
            return false;
        }
    }
    return true;
}

When testing the method validarNomeDuplicado, you want to know if the method procedimento was called. In this case, a spy of the object that has the method validarNomeDuplicado:

requisicao = Mockito.spy(new ProvTO());

And the test would look that way:

@Test
public void requisicaoInvalidaNomeDuplicado() {
    prov.add(umProvTO("Premio"));
    prov.add(umProvTO("Premio"));

    assertEquals(true, requisicao.validarNomeDuplicado());
    verify(requisicao).procedimento();
}

If the method procedimento not called will be thrown an error of

Wanted but not Invoked

In your case, the spy that you created does not seem to make sense. Regarding the method equals. In fact, the equals of ProvTO will not be called, because by calling the method Mockito.spy, a wrapper of the object ProvTO. When you make that call:

prov.add(umProvTO("Premio"))

you are passing another object that has another method equals. If you want to test the method equals real, you have to use the real object.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.