+ Start a Discussion
SolidLucasSolidLucas 

Class test help

Hello guys, i'm doing a test with a button that i created,this buttons changes the campaign status when clicked,my button is working,but i'm stucked into my test class. When i try to update the status on the test it fails, is not passing through my foreach. someone could help me?

My class
global class btn_EnviarCampanha {
	webservice static String btn_EnviarCampanha(Id mId) {

		String msg = '';


		try{

			List<CampaignMember> listMember = [SELECT Id,CampaignId,Status FROM CampaignMember WHERE Status = 'Aguardando Envio' AND CampaignId =:mId];
			List<CampaignMember> listMemberUpdated = new List<CampaignMember>();
	
			for(CampaignMember cm :listMember){
				cm.Status = 'Enviado';
				listMemberUpdated.add(cm);
			}

			update listMemberUpdated;
			msg = 'Email enviado com sucesso!';

		}catch(Exception ex){
			msg = 'Erro ao enviar mensagem!';
		} 
		return msg;  
	}
}
My test class
@isTest
private class tst_btn_EnviarCampanha {
    
    @isTest static void test_method_one() {
    
        Lead lead = new Lead(
            FirstName = 'Lucas',
            LastName = 'ze'
        );
        insert lead;

        Campaign cam = new Campaign(
            Name = 'Venda de Seminovos'
        );
        insert cam;

        CampaignMember membroCampanha = new CampaignMember(
            //Status = 'Aguardando Envio',
            CampaignId = cam.Id,
            LeadId = lead.Id 
        );
        insert membroCampanha;
        update membroCampanha;

        btn_EnviarCampanha.btn_EnviarCampanha(membroCampanha.Id);
    }   
}


 
RAM AnisettiRAM Anisetti
Hi,
try this...
 
@isTest
private class tst_btn_EnviarCampanha {
    
    @isTest static void test_method_one() {
    
        Lead lead = new Lead(
            FirstName = 'Lucas',
            LastName = 'ze'
        );
        insert lead;

        Campaign cam = new Campaign(
            Name = 'Venda de Seminovos'
        );
        insert cam;

        CampaignMember membroCampanha = new CampaignMember(
            Status = 'Aguardando Envio',
            CampaignId = cam.Id,
            LeadId = lead.Id 
        );
        insert membroCampanha;
        update membroCampanha;

        btn_EnviarCampanha.btn_EnviarCampanha(membroCampanha.Id);
    }   
}

 
SolidLucasSolidLucas
Hey,Ram!

I tried,but no success :( it keeps given me 69% of coverage.
RAM AnisettiRAM Anisetti
Let me know which lines are not covered??
SolidLucasSolidLucas
Sure!
User-added image
I don't know if you can see clearly,but these are the lines. 19,20,21,27,28
RAM AnisettiRAM Anisetti
@isTest
private class tst_btn_EnviarCampanha {
    
    @isTest static void test_method_one() {
    
        Lead lead = new Lead(
            FirstName = 'Lucas',
            LastName = 'ze'
        );
        insert lead;

        Campaign cam = new Campaign(
            Name = 'Venda de Seminovos'
        );
        insert cam;

        CampaignMember membroCampanha = new CampaignMember(
            Status = 'Aguardando Envio',
            CampaignId = cam.Id,
            LeadId = lead.Id 
        );
        insert membroCampanha;
       

        btn_EnviarCampanha.btn_EnviarCampanha(membroCampanha.Id);
    } 



  @isTest static void test_method_two() {
    
        Lead lead = new Lead(
            FirstName = 'Lucas',
            LastName = 'ze'
        );
        insert lead;

        Campaign cam = new Campaign(
            Name = 'Venda de Seminovos'
        );
        insert cam;

        CampaignMember membroCampanha = new CampaignMember(
            Status = 'Aguardando Envio',
            CampaignId = cam.Id,
            LeadId = lead.Id 
        );
        insert membroCampanha;
       
       //for catch block
        btn_EnviarCampanha.btn_EnviarCampanha('123456789');
    }	
}

 
RAM AnisettiRAM Anisetti
If it is not works...try this one
 
@isTest(seeAllData=true)
private class tst_btn_EnviarCampanha {
    
    @isTest static void test_method_one() {
	
	  List<CampaignMember> listMember = [SELECT Id,CampaignId,Status FROM CampaignMember WHERE Status = 'Aguardando Envio' limit 1];
      btn_EnviarCampanha.btn_EnviarCampanha(listMember[0].Id);
    }