function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC_EvolveSFDC_Evolve 

Test class Weird Behavior

I have a class  like 

 

Class Xyz {

 

public string password {get;set;}

public String confPassword {get;set;}

 

public PageReference submit() {

if(password ==null || password ==''){
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,Label.site.password);
ApexPages.addMessage(msg);
return null;
}

if(confPassword == null || password ==''){
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,Label.site.confirm_password);
ApexPages.addMessage(msg);
return null;
}

//Return error if passwords do not match.
 if(password != confPassword){
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,Label.site.passwords_dont_match);
ApexPages.addMessage(msg);
return null;
}

 

}

 

Now  When I try to Write  a Test class For it .. .I wrote it like below :_

 

system.Test.startTest();

// Create Instance of the class . .
XYZ  testNewUser9 = new XYZ();

// Create Instance of the class . .
xyz    testNewUser1 = new Xyz();


testNewUser1.password = 'hedhj';      //       Line  ABC
PageReference submitPage1 = testNewUser1.submit();

testNewUser1.password = '';  /// Line pqr
PageReference submitPage1a = testNewUser1.submit();

testNewUser1.password = 'Pass';
testNewUser9.confPassword = 'differ';
PageReference submitPage1b = testNewUser1.submit();


testNewUser1.password = 'Pass';
testNewUser9.confPassword = 'Pass';
PageReference submitPage1c = testNewUser1.submit();


system.Test.StopTest();

 

But the issue is that It always works for  Line ABC.... and took Confirn password NUll .. if I put Line PQR above.. It works as Password.......... Ideally   It should go to the every if block . 

 

Please let me know if i am doing something wrong  Thanks :)

SFDC_EvolveSFDC_Evolve

Please help in this . . Any help would be highly appreciated :)

Starz26Starz26

Well,

 

Not sure what you are asking as it was not very clear but since you are creating two instances of the class and you are always submitting using newuser1, newuser9 will always be null.

 

Maybe setting the class variables to static will solve your issue as each instance of the class will be using the same values......