-2
I have a main class Usuario
extending (extends
) an abstract class called Sessao
, and uses a function of Sessao
calling for obterDados()
that takes session data and returns to an object SessaoTO
. I need to mock this function so it returns the mocked object and follow the flow.
I’ve tried mocking the class Sessao
as follows:
// Declaração feita na classe
@Mock
Sessao sessao;
...
// Declaração feita na função de teste
SessaoTO sessaoTO = new SessaoTO();
sessaoTO.setCpf("47566611100");
sessaoTO.setNome("Gaus");
sessaoTO.setSigla("user");
when(sessao.obterDados()).thenReturn(sessaoTO);
The problem is that at the time of execution is giving Nullpointer error because this mock is not working. I have tried using the @InjectMocks
, but it didn’t work.
Hello! Share the code of the User and Session class if possible. I confess that, already in advance, User extend a class called Sessao does not seem to me a correct approach.
– Dherik
I didn’t implement the class code. I’m just doing the unit tests.
– Isdeniel
I don’t know if mockite allows private method mocking in newer versions, but you can do it using powermock: https://github.com/powermock/powermock/wiki/Mockito#how-to-Verify-private-behavior
– hkotsubo
Closely related but not duplicated: https://answall.com/q/269196/132
– Victor Stafusa