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
Carter85Carter85 

Need help fixing a test class

I'm trying to finish a test class for a process which issues credentials for users and I'm a bit stuck at the moment.
This is the class:
public with sharing class CM_InfoController{

	private static String ORG_WIDE_EMAIL_ADDRESS = 'customerservice@maximusautogroup.com';

    public Contact user    {get;set;}
    public Contact oUser    {get;set;}
    public Contact nu   {get;set;}
    public UserInfo__c upd	{get;set;}
        
    private static ID orgWideEmailId = [SELECT id FROM OrgWideEmailAddress WHERE address=:ORG_WIDE_EMAIL_ADDRESS].id;

    public CM_InfoController(){
    }

    public CM_InfoController(ApexPages.StandardController controller){
        user = (Contact)controller.getRecord();
        }

    public PageReference sendEmail(){
        if(user != null){
            nu = [SELECT UserInfo__c, ID, Name, Associated_With__c, Is_Dealer__c, Is_Agent__c, Email, Password__c FROM Contact WHERE id=:user.id];
            if(nu.Email == null){
                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Error, 'Please make sure that the email field has a value in it before trying to issue credentials.' );
                ApexPages.addMessage(myMsg);
                return null;
                }
            if(nu.userInfo__c == null){
                try{
                    UserInfo__c newUser = new UserInfo__c();
                    newUser.Account__c = nu.Associated_With__c;
                    newUser.UserId__c = nu.Email;
                    newUser.Password__c = nu.Password__c;
                    newUser.Assigned_To__c = nu.ID;
                    newUser.Dealer__c = nu.Is_Dealer__c;
                    newUser.Agent__c = nu.Is_Agent__c;

                    insert newUser;
                    nu.UserInfo__c = newUser.id;
                    update nu;
                    }
                catch(DmlException e){
                    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.Error, 'This user has already been given credentials, or this email has been assigned to someone else, change the existing information from the UserInfo tab before attempting to resend if this is in error.' );
                    ApexPages.addMessage(myMsg);
                    return null;
                    }
                    buildEmail();
                    }
                else{
                    reSend();
                    }
                    }
                    return null;
                    }

            public void reSend(){
                Contact nu = [SELECT Associated_With__c, UserInfo__c, Email, Password__c FROM Contact WHERE id=:user.id];
                UserInfo__c upd = [SELECT Name FROM UserInfo__c WHERE id=:nu.UserInfo__c];
                    upd.UserId__c = nu.Email;
                    upd.Password__c = nu.Password__c;

                  update upd;
                  buildEmail();
                  }
                  
            public void buildEmail(){
                  // Build new URL
                  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                  String htmlBody = '<h1>MAG User Info Request</h1>' +
                  '<p>Dear ' + nu.Name + ',</p>' +
                  '<p>We have received your request for user credential information in order to access some secure features of the Maximus Auto Group website based on your affiliation with ' + nu.Associated_With__c + '.</p>' +
                  '<p>Your Username is: ' + nu.Email + '</p>' +
                  '<p>Your Password is: ' + nu.Password__c + '</p>' +
                  '<p>As the password contained here is only meant to be temporary, we recommend changing it to something easier for you to remember at your earliest convenience.</p>' +
                  '<p>You may change your associated email or password from any of the secure pages on the website by pressing the change password button.</p>' +
                  '<p>You may access some of the restricted features, based on the level of access granted to you, by visiting our website and navigating to the feature in question: <br/>http://MaximusAutoGroup.com</p>' +
                  '<p>If you have any questions or concerns regarding this information, please contact one of our associates at 1-800-801-1342 and we will assist you, or reply to this email with any specific questions.</p>' +
                  '<p>Thank you for your business and continued support!</p>' +
                  '<p>MAXIMUS AUTO GROUP</p>';

                   //Build Email
                   mail.setOrgWideEmailAddressId(orgWideEmailId);
                            String[] toAddresses = new String[]{nu.Email};
                                            mail.setToAddresses(toAddresses);
                                            mail.setSubject('MAG User Access');
                                            mail.setHtmlBody(htmlBody);

                                            Messaging.sendEmail(new Messaging.Email[]{mail},true);

                                            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Message sent successfully');
                                            ApexPages.addMessage(msg);
                }
}
And this is the test:
@isTest(SeeAllData=true)
public with sharing class CM_InfoTest {

	public static testMethod void CM_InfoTestTst(){

		Contact use = new Contact(
								  FirstName = 'Tom',
								  LastName = 'Testington',
								  Account = [Select ID, Name FROM Account WHERE Name =:'Atlantic Dealer Services'],
								  Email = ''
								  );
								  insert use;

		CM_InfoController controller = new CM_InfoController();
		controller = new CM_InfoController(new ApexPages.StandardController(use));
		controller.sendEmail();

		use.Email = 'testing@maximusautogroup.com';
		update use;

		UserInfo__c tst = new UserInfo__c(
										  Account__c = use.Account.Name,
										  Assigned_to__c = use.ID,
										  Password__c = use.Password__c,
										  UserId__c = use.Email
										  );
										  insert tst;

		controller.sendEmail();
		controller.buildEmail();
		controller.nu.UserInfo__c = tst.ID;
		system.debug(controller.nu);
		system.debug(controller.nu.UserInfo__c);
		controller.reSend();
		}

}
I'm stuck at 83% at the moment because the reSend() method keeps throwing an error when the test controller invokes it, claiming: FATAL_ERROR Class.CM_InfoController.reSend: line 57, column 1
FATAL_ERROR Class.CM_InfoTest.CM_InfoTestTst: line 34, column 1
FATAL_ERROR System.QueryException: List has no rows for assignment to SObject
And I'm puzzled because my debug statements are showing that the variable going into that query is not null and should therefore be returning a result.
If anyone has any suggestions, or a means of covering the class better than I am currently that would be appreciated.
ShashankShashank (Salesforce Developers) 
Try removing seeAllData=true and create your test data within the class itself. This could be the reason for your issue.
Carter85Carter85
Thanks for the suggestion.  It was actually an error with a required field interfering with the creation of the associated record during the test.  I just made it no longer required as it wasn't necessary and that fixed the issue.