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
Shannon Andreas 1Shannon Andreas 1 

Help with Apex test code

Hello,

I hope this is an easy one. I was trying deploy a change set and ran in to the following error when trying to validate:

TEST_MassTaskController.myUnitTest(), Details: System.Exception: Assertion Failed: Same value: null Class.TEST_MassTaskController.myUnitTest: line 110, column 1

Here is the test code:

@isTest
private class TEST_MassTaskController {

    static testMethod void myUnitTest() {
        Test.startTest();
        
        //Create Accounts
        Account account1 = new Account();
        account1.Name = 'Test_Account_01';
        insert account1;
        
        Account account2 = new Account();
        account2.Name = 'Test_Account_02';
        insert account2;        
        
        //Create Contacts
        Contact contact1 = new Contact();
        contact1.LastName = 'Test_Contact_01';
        insert contact1;
        
        Contact contact2 = new Contact();
        contact2.LastName = 'Test_Contact_01';
        insert contact2;
        
        //Get a profile from SFDC
        Profile profile = [select Id from Profile limit 1];
        
        //Create a user
        User user = new User();
        user.Username = 'Test_user_name@test.com';
        user.LastName = 'Test_last_name';
        user.ProfileId = profile.Id;
        user.Alias = 'tst';
        user.Email = 'Test_email@email.com';
        user.CommunityNickname = 'Test_nick_name';
        user.TimeZoneSidKey = 'GMT';
        user.LocaleSidKey = 'en_US';
        user.LanguageLocaleKey = 'en_US';
        user.EmailEncodingKey = 'ISO-8859-1';
        insert user;
        
        //Simulate the page for What Id
        PageReference pPageReference = Page.Mass_Task_Action;
        pPageReference.getParameters().put('objIds',account1.Id+','+account2.Id);
        pPageReference.getParameters().put('retUrl','');
        Test.setCurrentPage(pPageReference);
        
        MassTaskController controler = new MassTaskController();
        System.assertEquals(controler.showWhoId, true);
        controler.getTableDisplayNames();
        controler.saveNew();
        controler.save();
        controler.back();

        //Simulate the page for Who Id
        pPageReference = Page.Mass_Task_Action;
        pPageReference.getParameters().put('objIds',contact1.Id+','+contact2.Id);
        pPageReference.getParameters().put('retUrl','');
        Test.setCurrentPage(pPageReference);
        controler = new MassTaskController();
        System.assertEquals(controler.showWhoId, false);
        controler.getTableDisplayNames();
        controler.getselReminderOptions();
        controler.saveNew();
        Pagereference pageRef = controler.save();
        System.assertEquals(pageRef, null);
        controler.back();
        
        controler.task.OwnerId = user.Id;
        controler.task.Subject = 'Test_Subject';
        controler.task.Status = 'Completed';
        controler.task.Priority = 'High';
        //Set the reminder
        controler.task.IsReminderSet = true;
        controler.contact.Birthdate = Date.today();
        controler.reminderTime = '23:30';
        //Send Email notification
        controler.sendNotificationEmailCheckBox = true;
        
        controler.saveNew();
        pageRef = controler.save();
110   System.assertNotEquals(pageRef, null);
        
        Test.stopTest();
    }
}

I have searched these boards all day today and found no one referencing the System.assertNotEquals. Can anyone explain what that is stating? Also, can you help me to determine where I am missing something? Let me know if you need the Apex Class this is referencing.

Thanks,

Shannon
Best Answer chosen by Shannon Andreas 1
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Shannon,

It looks like there is null value.
And code coverage is only calculated if your test method runs successfully.
Please try below code.
@isTest
private class TEST_MassTaskController {

    static testMethod void myUnitTest() {
        Test.startTest();
        
        //Create Accounts
        Account account1 = new Account();
        account1.Name = 'Test_Account_01';
        insert account1;
        
        Account account2 = new Account();
        account2.Name = 'Test_Account_02';
        insert account2;        
        
        //Create Contacts
        Contact contact1 = new Contact();
        contact1.LastName = 'Test_Contact_01';
        insert contact1;
        
        Contact contact2 = new Contact();
        contact2.LastName = 'Test_Contact_01';
        insert contact2;
        
        //Get a profile from SFDC
        Profile profile = [select Id from Profile limit 1];
        
        //Create a user
        User user = new User();
        user.Username = 'Test_user_name@test.com';
        user.LastName = 'Test_last_name';
        user.ProfileId = profile.Id;
        user.Alias = 'tst';
        user.Email = 'Test_email@email.com';
        user.CommunityNickname = 'Test_nick_name';
        user.TimeZoneSidKey = 'GMT';
        user.LocaleSidKey = 'en_US';
        user.LanguageLocaleKey = 'en_US';
        user.EmailEncodingKey = 'ISO-8859-1';
        insert user;
        
        //Simulate the page for What Id
        PageReference pPageReference = Page.Mass_Task_Action;
        pPageReference.getParameters().put('objIds',account1.Id+','+account2.Id);
        pPageReference.getParameters().put('retUrl','');
        Test.setCurrentPage(pPageReference);
        
        MassTaskController controler = new MassTaskController();
        System.assertEquals(controler.showWhoId, true);
        controler.getTableDisplayNames();
        controler.saveNew();
        controler.save();
        controler.back();

        //Simulate the page for Who Id
        pPageReference = Page.Mass_Task_Action;
        pPageReference.getParameters().put('objIds',contact1.Id+','+contact2.Id);
        pPageReference.getParameters().put('retUrl','');
        Test.setCurrentPage(pPageReference);
        controler = new MassTaskController();
        System.assertEquals(controler.showWhoId, false);
        controler.getTableDisplayNames();
        controler.getselReminderOptions();
        controler.saveNew();
        Pagereference pageRef = controler.save();
        System.assertEquals(pageRef, null);
        controler.back();
        
        controler.task.OwnerId = user.Id;
        controler.task.Subject = 'Test_Subject';
        controler.task.Status = 'Completed';
        controler.task.Priority = 'High';
        //Set the reminder
        controler.task.IsReminderSet = true;
        controler.contact.Birthdate = Date.today();
        controler.reminderTime = '23:30';
        //Send Email notification
        controler.sendNotificationEmailCheckBox = true;
        
        controler.saveNew();
        pageRef = controler.save();
        System.assertEquals(pageRef, null);
        
        Test.stopTest();
    }
}

Let us know if it helps you.

All Answers

Shannon Andreas 1Shannon Andreas 1
BTW, the test method is failing because it is only offering 66% coverage.
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Shannon,

It looks like there is null value.
And code coverage is only calculated if your test method runs successfully.
Please try below code.
@isTest
private class TEST_MassTaskController {

    static testMethod void myUnitTest() {
        Test.startTest();
        
        //Create Accounts
        Account account1 = new Account();
        account1.Name = 'Test_Account_01';
        insert account1;
        
        Account account2 = new Account();
        account2.Name = 'Test_Account_02';
        insert account2;        
        
        //Create Contacts
        Contact contact1 = new Contact();
        contact1.LastName = 'Test_Contact_01';
        insert contact1;
        
        Contact contact2 = new Contact();
        contact2.LastName = 'Test_Contact_01';
        insert contact2;
        
        //Get a profile from SFDC
        Profile profile = [select Id from Profile limit 1];
        
        //Create a user
        User user = new User();
        user.Username = 'Test_user_name@test.com';
        user.LastName = 'Test_last_name';
        user.ProfileId = profile.Id;
        user.Alias = 'tst';
        user.Email = 'Test_email@email.com';
        user.CommunityNickname = 'Test_nick_name';
        user.TimeZoneSidKey = 'GMT';
        user.LocaleSidKey = 'en_US';
        user.LanguageLocaleKey = 'en_US';
        user.EmailEncodingKey = 'ISO-8859-1';
        insert user;
        
        //Simulate the page for What Id
        PageReference pPageReference = Page.Mass_Task_Action;
        pPageReference.getParameters().put('objIds',account1.Id+','+account2.Id);
        pPageReference.getParameters().put('retUrl','');
        Test.setCurrentPage(pPageReference);
        
        MassTaskController controler = new MassTaskController();
        System.assertEquals(controler.showWhoId, true);
        controler.getTableDisplayNames();
        controler.saveNew();
        controler.save();
        controler.back();

        //Simulate the page for Who Id
        pPageReference = Page.Mass_Task_Action;
        pPageReference.getParameters().put('objIds',contact1.Id+','+contact2.Id);
        pPageReference.getParameters().put('retUrl','');
        Test.setCurrentPage(pPageReference);
        controler = new MassTaskController();
        System.assertEquals(controler.showWhoId, false);
        controler.getTableDisplayNames();
        controler.getselReminderOptions();
        controler.saveNew();
        Pagereference pageRef = controler.save();
        System.assertEquals(pageRef, null);
        controler.back();
        
        controler.task.OwnerId = user.Id;
        controler.task.Subject = 'Test_Subject';
        controler.task.Status = 'Completed';
        controler.task.Priority = 'High';
        //Set the reminder
        controler.task.IsReminderSet = true;
        controler.contact.Birthdate = Date.today();
        controler.reminderTime = '23:30';
        //Send Email notification
        controler.sendNotificationEmailCheckBox = true;
        
        controler.saveNew();
        pageRef = controler.save();
        System.assertEquals(pageRef, null);
        
        Test.stopTest();
    }
}

Let us know if it helps you.
This was selected as the best answer
Shannon Andreas 1Shannon Andreas 1
If I tell you that I was going to do that, would you believe me? J
 
Thank you!!! It worked.
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Offcourse Shannon !!
 
Shannon Andreas 1Shannon Andreas 1
Ashish,

My code coverage went to 0%, but the test passed. What gives?