You need to sign in to do that
Don't have an account?
SolidLucas
Test Class Help
Well guys my code is 88% covered but i want to increase more this value, but i'm stucked in some points for example i would like to know how can i test the message error of my method isEmpty,and how to test the insert into the list of my doSave method. if someone could give me example i would be very thankful!
My apexClass
My apexClass
public with sharing class PaymentManager { /* * PRIVATE VARIABLES */ private Map<Id, MetodoPagamentoEndereco__c> mapPaymentsOriginal; /* * PUBLIC VARIABLES */ public String title {get;set;} public Payment_cls paymentAux {get;set;} public List<Payment_cls> listPaymentsAddress {get;set;} public Integer selectedPrincipalId {get;set;} public Integer paymentIdSelected {get;set;} /* * SUBCLASSES */ private class Payment_cls { public Integer id {get;set;} public MetodoPagamentoEndereco__c payment {get;set;} public boolean isDeleted {get;set;} public Payment_cls() { payment = new MetodoPagamentoEndereco__c(); } } /* * CONSTRUCTOR */ public PaymentManager(){ title = 'Métodos de Pagamento'; paymentAux = new Payment_cls(); } /* * PRIVATE METHODS */ /* * este método identifica se já exsite um método de pagamento cadastrado para o endereço. */ private boolean hasMethod(String pMethodId){ boolean result = false; for (Payment_cls iPayment: listPaymentsAddress){ if (iPayment.payment.MetodoPagamento__c == pMethodId) { result = true; break; } } return result; } private boolean isEmpty(list<Payment_cls> pList){ boolean result = false; if (pList.isEmpty()) { ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR,'É necessårio informar ao menos um método de pagamento para prosseguir.')); result = true; } return result; } private boolean existingPaymentIsValid(MetodoPagamentoEndereco__c pMetodo){ boolean result = true; MetodoPagamentoEndereco__c old = mapPaymentsOriginal.get(pMetodo.Id); if (pMetodo.Inicio__c < old.Inicio__c) { pMetodo.Inicio__c.addError('Não é permitido alterar a data de início para uma data anterior a previamente cadastrada: ' + old.Inicio__c.format()); result = false; } else result = finalDateIsMajor(pMetodo); return result; } private boolean newerPaymentIsValid(MetodoPagamentoEndereco__c pMetodo){ boolean result = true; if (pMetodo.Inicio__c < Date.Today()) { pMetodo.Inicio__c.addError('Não é permitido o cadastramento de um novo método de pagamento com data retroativa.'); result = false; } else result = finalDateIsMajor(pMetodo); return result; } private boolean finalDateIsMajor(MetodoPagamentoEndereco__c pMetodo){ boolean result = true; if ( pMetodo.Fim__c != null){ if (pMetodo.Inicio__c > pMetodo.Fim__c) { pMetodo.Inicio__c.addError('A data de início não pode ser maior que a data final.'); result = false; } } return result; } private Integer getMajorId() { Integer retorno = 0; for (Payment_cls icPayment : listPaymentsAddress) { if (icPayment.id > retorno) { retorno = icPayment.id; } } return retorno + 1; } /* * PUBLIC METHODS */ public void loadPaymentsByAddress(Endere_o_Relacionado__c pEndereco) { if (listPaymentsAddress == null) { listPaymentsAddress = new List<Payment_cls>(); if (Functions.isNotEmptyOrNull(pEndereco.Id)) { list<MetodoPagamentoEndereco__c> listPaymentsOriginal = [ SELECT Id, Name, Inicio__c, Fim__c, Principal__c, MetodoPagamento__c FROM MetodoPagamentoEndereco__c WHERE Endereco__c = :pEndereco.Id ORDER BY Name ]; if (!listPaymentsOriginal.isEmpty()) { Integer i = 1; mapPaymentsOriginal = new Map<Id,MetodoPagamentoEndereco__c >(); for (MetodoPagamentoEndereco__c iPayment : listPaymentsOriginal) { mapPaymentsOriginal.put(iPayment.Id, iPayment.clone(true, true, true, true)); Payment_cls cPayment = new Payment_cls(); cPayment.payment = iPayment;//.clone(true, true, true, true); cPayment.id = i; cPayment.isDeleted = false; listPaymentsAddress.add(cPayment); //setando o Id do método pagamento selecionado como principal. if (cPayment.payment.Principal__c) { selectedPrincipalId = cPayment.Id; } i++; } } } } } public PageReference deletePayment() { for (Payment_cls icPayment : listPaymentsAddress) { if (icPayment.id == paymentIdSelected) { icPayment.isDeleted = !icPayment.isDeleted; break; } } return null; } public PageReference savePaymentInList() { paymentAux = new Payment_cls(); String methodPaymentId = Apexpages.currentPage().getParameters().get('methodPaymentId'); String methodPaymentName = Apexpages.currentPage().getParameters().get('methodPaymentName'); if (hasMethod(methodPaymentId)) { ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR,'O método de pagamento "' + methodPaymentName + '" já está cadastrado.')); } else { paymentAux.payment = new MetodoPagamentoEndereco__c( Inicio__c = Date.today(), Principal__c = true, MetodoPagamento__c = methodPaymentId, Name = methodPaymentName ); Payment_cls cPayment = new Payment_cls(); cPayment = paymentAux; //cPayment.payment.Principal__c = listPaymentsAddress.isEmpty(); cPayment.id = getMajorId(); cPayment.isDeleted = false; listPaymentsAddress.add(cPayment); selectedPrincipalId = cPayment.id ; } return null; } public void doSelectPrincipal() { for (Payment_cls icPayment : listPaymentsAddress) { boolean isPrincipal = icPayment.id == selectedPrincipalId; icPayment.payment.Principal__c = isPrincipal; } } public boolean isValid(){ boolean result = true; result = !isEmpty(listPaymentsAddress); if (result) { list<Payment_cls> listWithoutDelete = new list<Payment_cls>(); for (Payment_cls iPayment :listPaymentsAddress){ if (!iPayment.isDeleted) { listWithoutDelete.add(iPayment); if (Functions.IsNotEmptyOrNull(iPayment.payment.Id)){ if (!existingPaymentIsValid(iPayment.payment)) result = false; } else { if (!newerPaymentIsValid(iPayment.payment)) result = false; } } } if (result ) result = !isEmpty(listWithoutDelete); } return result; } public void doSave(Endere_o_Relacionado__c pEndereco) { if (pEndereco != null && Functions.isNotEmptyOrNull(pEndereco.Id)) { List<MetodoPagamentoEndereco__c> auxListInsertPayment = new List<MetodoPagamentoEndereco__c>(); List<MetodoPagamentoEndereco__c> auxListUpdatePayment = new List<MetodoPagamentoEndereco__c>(); List<MetodoPagamentoEndereco__c> auxListDeletePayment = new List<MetodoPagamentoEndereco__c>(); for (Payment_cls cPayment : listPaymentsAddress) { if (!Functions.isNotEmptyOrNull(cPayment.payment.Id)) { if (!cPayment.isDeleted) { cPayment.payment.Endereco__c = pEndereco.Id; auxListInsertPayment.add(cPayment.payment); } } else { if (cPayment.isDeleted) { auxListDeletePayment.add(cPayment.payment); } else { auxListUpdatePayment.add(cPayment.payment); } } } if (!auxListDeletePayment.isEmpty()) { delete auxListDeletePayment; } if (!auxListUpdatePayment.isEmpty()) { update auxListUpdatePayment; } if (!auxListInsertPayment.isEmpty()) { insert auxListInsertPayment; } } } }My testClass
private class PaymentManager_tst { static testMethod void myUnitTest() { //istancia do construtor PaymentManager payment = new PaymentManager(); List<MetodoPagamentoEndereco__c> MetodoPagamentoEnderecoVazio = new List<MetodoPagamentoEndereco__c>(); Endere_o_Relacionado__c endereco = new Endere_o_Relacionado__c( Name = 'teste' ); insert endereco; MetodoPagamento__c metodoPagamento = new MetodoPagamento__c( Codigo__c = 'teste', Fim__c = Date.valueOf(Datetime.now().addDays(-10)), Inicio__c = Date.valueOf(Datetime.now().addDays(-9)), Observacao__c = 'teste de Observação' ); insert metodoPagamento; MetodoPagamentoEndereco__c metodoPagamentoEndereco = new MetodoPagamentoEndereco__c( Name = 'Teste', Endereco__c = endereco.Id, Fim__c = Date.valueOf(Datetime.now().addDays(-10)), Inicio__c = date.today(), Principal__c = true, MetodoPagamento__c = metodoPagamento.Id ); insert metodoPagamentoEndereco; //Métodos publicos do construtor payment.loadPaymentsByAddress(endereco); payment.doSave(endereco); payment.savePaymentInList(); payment.savePaymentInList(); payment.isValid(); payment.doSelectPrincipal(); payment.deletePayment(); } }
Hope this helps!
All Answers
Hope this helps!