I have this class:
package br.com.lumera.notificato.api.central.o;
import br.com.lumera.cec.modelmapper.mapping.vo.AcessoCentral;
import br.com.lumera.cec.modelmapper.model.Cartorio;
import br.com.lumera.cec.modelmapper.model.EmailRequestVO;
import br.com.lumera.cec.modelmapper.model.pedido.*;
import br.com.lumera.notificato.api.central.Central;
import br.com.lumera.notificato.api.central.Filtro;
import br.com.lumera.notificato.api.central.FiltroOnr;
import br.com.lumera.notificato.api.central.exceptions.CentralException;
import br.com.lumera.notificato.api.exception.ValidationException;
import br.com.lumera.notificato.api.service.EmailService;
import br.com.lumera.notificato.api.service.ServicoService;
import br.com.lumera.notificato.api.util.Utils;
import br.com.lumera.onr.wsoficio.certidao.CertidaoFacade;
import br.com.lumera.onr.wsoficio.certidao.ONREnvioAnexoException;
import br.com.lumera.onr.wsoficio.certidao.model.PedidoCertidao;
import br.com.lumera.onr.wsoficio.certidao.model.StatusCertidao;
import br.com.lumera.onr.wsoficio.certidao.model.TipoCertidao;
import br.com.lumera.onr.wsoficio.certidao.model.TipoCobranca;
import br.com.lumera.onr.wsoficio.exception.ONRDevolverCertidaoException;
import br.com.lumera.onr.wsoficio.exception.ONRFinalizarRespostaCertidaoException;
import br.com.lumera.onr.wsoficio.exception.ONRInformarCustasCertidaoException;
import br.com.lumera.onr.wsoficio.login.exception.ONRLoginInvalidoException;
import br.com.lumera.onr.wsoficio.login.exception.ONRUsuarioInativoException;
import br.com.lumera.onr.wsoficio.login.ws.LoginConfig;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
@Slf4j
public class CentralO implements Central {
@Autowired
private PedidoCertidaoOMapper pedidoCertidaoOMapper;
@Autowired
private ObjectMapper jacksonObjectMapper;
@Autowired
private EmailService emailService;
private CertidaoFacade certidaoFacade;
protected CertidaoFacade preencherDadosAcesso(AcessoCentral acessoCentral) throws CentralException {
try {
return certidaoFacade.login(LoginConfig.builder()
.chave(acessoCentral.getDadosAcesso().get("chave"))
.cn(acessoCentral.getDadosAcesso().get("cn"))
.cpf(acessoCentral.getDadosAcesso().get("cpf"))
.email(acessoCentral.getDadosAcesso().get("email"))
.idParceiro(Integer.parseInt(acessoCentral.getDadosAcesso().get("idParceiro")))
.issuer(acessoCentral.getDadosAcesso().get("issuer"))
.publickey(acessoCentral.getDadosAcesso().get("publicKey"))
.serialnumber(acessoCentral.getDadosAcesso().get("serialNumber"))
.validUntil(acessoCentral.getDadosAcesso().get("validUntil"))
.build());
} catch (OUsuarioInativoException e) {
throw new CentralException("Usuário inativo", e.getMessage());
} catch (OLoginInvalidoException e) {
throw new CentralException("Login inválido", e.getMessage());
}
}
@Override
public void enviarCustas(AcessoCentral acessoCentral, DadosCentral dadosCentral, BigDecimal valor) throws CentralException {
try {
log.debug("Pedido {} deve enviar valores: {}", dadosCentral.getIdentificacao(), valor);
preencherDadosAcesso(acessoCentral).informarCustasCertidao(dadosCentral.getIdentificacao(), valor);
} catch (OInformarCustasCertidaoException e) {
throw new RuntimeException(e);
}
}
}
and this test class that works fine.
package br.com.lumera.notificato.api.central.o;
import br.com.lumera.cec.modelmapper.mapping.vo.AcessoCentral;
import br.com.lumera.cec.modelmapper.model.pedido.DadosCentral;
import br.com.lumera.notificato.api.central.Central;
import br.com.lumera.notificato.api.central.exceptions.CentralException;
import br.com.lumera.onr.wsoficio.certidao.CertidaoFacade;
import br.com.lumera.onr.wsoficio.exception.ONRInformarCustasCertidaoException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import java.math.BigDecimal;
import java.util.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.*;
@ExtendWith(MockitoExtension.class)
class CentralOTest {
@Mock
CertidaoFacade certidaoFacade;
@InjectMocks
@Spy
CentralO centralO;
AcessoCentral acessoCentral;
@BeforeEach
void setUp() throws Exception {
HashMap<String, String> map = new HashMap<>();
map.put("chave","1" );
map.put("cn","1");
map.put("cpf","1");
map.put("email","1");
map.put("idParceiro","1");
map.put("issuer","1");
map.put("publicKey","1");
map.put("serialNumber","1");
map.put("validUntil","1");
acessoCentral = AcessoCentral.builder()
.nome(Central.CENTRAL.O.name())
.dadosAcesso(map)
.build();
given(certidaoFacade.login(any())).willReturn(certidaoFacade);
}
@Test
void enviarCustas() throws OInformarCustasCertidaoException, CentralException {
DadosCentral dadosCentral = DadosCentral.builder().identificacao("S2134351313153").build();
willDoNothing().given(certidaoFacade).informarCustasCertidao(dadosCentral.getIdentificacao(), BigDecimal.ZERO);
centralO.enviarCustas(acessoCentral, dadosCentral, BigDecimal.ZERO);
}
}
But now I need to change in my CentraO class from this:
private CertidaoFacade certidaoFacade;// = new CertidaoFacade();
protected CertidaoFacade preencherDadosAcesso(AcessoCentral acessoCentral) throws CentralException {
try {
return certidaoFacade.login(LoginConfig.builder()
.chave(acessoCentral.getDadosAcesso().get("chave"))
.cn(acessoCentral.getDadosAcesso().get("cn"))
.cpf(acessoCentral.getDadosAcesso().get("cpf"))
.email(acessoCentral.getDadosAcesso().get("email"))
.idParceiro(Integer.parseInt(acessoCentral.getDadosAcesso().get("idParceiro")))
.issuer(acessoCentral.getDadosAcesso().get("issuer"))
.publickey(acessoCentral.getDadosAcesso().get("publicKey"))
.serialnumber(acessoCentral.getDadosAcesso().get("serialNumber"))
.validUntil(acessoCentral.getDadosAcesso().get("validUntil"))
.build());
} catch (ONRUsuarioInativoException e) {
throw new CentralException("Usuário inativo", e.getMessage());
} catch (ONRLoginInvalidoException e) {
throw new CentralException("Login inválido", e.getMessage());
}
}
to this:
protected CertidaoFacade preencherDadosAcesso(AcessoCentral acessoCentral) throws CentralException {
try {
return new CertidaoFacade().login(LoginConfig.builder()
.chave(acessoCentral.getDadosAcesso().get("chave"))
.cn(acessoCentral.getDadosAcesso().get("cn"))
.cpf(acessoCentral.getDadosAcesso().get("cpf"))
.email(acessoCentral.getDadosAcesso().get("email"))
.idParceiro(Integer.parseInt(acessoCentral.getDadosAcesso().get("idParceiro")))
.issuer(acessoCentral.getDadosAcesso().get("issuer"))
.publickey(acessoCentral.getDadosAcesso().get("publicKey"))
.serialnumber(acessoCentral.getDadosAcesso().get("serialNumber"))
.validUntil(acessoCentral.getDadosAcesso().get("validUntil"))
.build());
} catch (ONRUsuarioInativoException e) {
throw new CentralException("Usuário inativo", e.getMessage());
} catch (ONRLoginInvalidoException e) {
throw new CentralException("Login inválido", e.getMessage());
}
}
but with this change I cannot mock the new instace of CertidaoFacade. It's possible to still using all my mocked (when) with this new instance?