How to layout Restriction.eq () using Mockito

I am having trouble creating tests with Mockito. During debugging with Eclipse, I got null from Restrictions.eq , I took every step to create a static layout with Restrictions :

 @RunWith(PowerMockRunner.class) @PrepareForTest({ RequisicaoList.class, StatusMessages.class, FacesMessages.class, Restrictions.class }) public class RequisicaoListTest { ... @SuppressWarnings("unchecked") public String criteriaContigencia() { Criteria criteria = criarCriteria(Requisicao.class); criteria.createAlias("produto", "prod"); criteria.add(Restrictions.eq("prod.ar",arSelecionada)); if (getExemplo().getNrProtocolo() != null) criteria.add(Restrictions.eq("nrProtocolo", getExemplo() .getNrProtocolo())); situacoesPesquisa.clear(); situacoesPesquisa.add(SituacaoRequisicao.PENDENTE_PAGAMENTO); situacoesPesquisa.add(SituacaoRequisicao.PENDENTE_AGENDAMENTO); if (!situacoesPesquisa.isEmpty()) { criteria.add(Restrictions.in("situacao", situacoesPesquisa)); } if (getExemplo().getResponsavel() != null && StringUtils.isNotBlank(getExemplo().getResponsavel().getCpf())) { criteria = criteria.createCriteria("responsavel"); criteria = criteria.add(Restrictions.eq("cpf", getExemplo().getResponsavel().getCpf())); } resultado = Collections.checkedList(criteria.list(),Requisicao.class); return null; } 

My code mixes managedBean with persistence level, I cannot change it, but I have to create tests for this project.

The problem is to mock

 Restrictions.eq("cpf", getExemplo().getResponsavel().getCpf()) 

because even i do:

 SimpleExpression simpleExpressionEq = mock(SimpleExpression.class); PowerMockito.mockStatic(Restrictions.class); when(Restrictions.eq("cpf", "00000000091")).thenReturn(simpleExpressionEq); 

then I still get a null return from Restriction.eq , even passing spectral values. But during debugging, if I express an expression in an expression view,

 Restrictions.eq("cpf", "00000000091")) 

It works fine and returns the mock SimpleExpression

+4
source share
2 answers

I don’t think it makes sense to make fun of Restrictions when testing this method. The purpose of this method is to wrap access to data (Hibernate and any database that you use). Thus, a useful test for this method would be a test that ensures that you are using the database correctly.

So, instead of writing a unit test where everything is mocking, I would recommend writing an integration test. Use an in-memory database such as H2 (http://h2database.com); and write a test that sets up the relevant data, and then actually selects it. Then your level of confidence that this method really does what it should do will far exceed what unit test will give you.

+2
source

If

 Restrictions.eq("cpf", "00000000091")) 

returns correctly, then maybe the problem is with getExemplo().getResponsavel().getCpf() . Are you sure it returns "00000000091"? You did not provide enough code to find out if this might be a problem, but worth exploring.

Another approach would be to avoid bullying Restrictions in general. When I want to do something like this, I use the class of real constraints and let it return the real criterion. I wrote a simple "toStringEq" that I can use to bully or test the behavior of a Criteria object. Of course, matching on the toString () object does not match the match for the actual equality, but since Criterion objects use instance equality for equality, but for Criterion objects that have the same toString () value, are functionally equivalent, ve found it quite convenient for this case use.

For example, after your call, if you have a metch Criteria object, you can:

 verify(mockCriteria).add(argThat(toStringEq(Restrictions.eq("cpf", "0000000091")))); 

Here is a simple class class:

 import org.hamcrest.Description; import org.mockito.ArgumentMatcher; public class HamcrestToStringMatcher<T> extends ArgumentMatcher<T> { private T toMatch; public HamcrestToStringMatcher(T toMatch) { this.toMatch = toMatch; } @Override public void describeTo(Description description) { description.appendText(toMatch == null ? "null" : toMatch.toString()); } @Override public boolean matches(Object argument) { return ((toMatch == null && argument == null) || (toMatch != null && argument != null && toMatch.toString().equals( argument.toString()))); } public static <T> HamcrestToStringMatcher<T> toStringEq(T toMatch) { return new HamcrestToStringMatcher<T>(toMatch); } } 
+1
source

Source: https://habr.com/ru/post/1402466/


All Articles