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
Marty LavenderMarty Lavender 

Help writing a simple unit test to validate my code

I found several articles on building a custom login controller and custom login page for SalesForce Communities. My issue now is that I am missing the 75% code coverage mark by 2 - 3% so I am unable to deploy my new controllers to Production.

I have a CustomLogin controller with the following code:

global with sharing class CustomLoginController {
    global String username {get; set;}
    global String password {get; set;}
    global CustomLoginController () {}
    global PageReference login() {
        return Site.login(username, password, null);
    }
}

I am needing to write a unit test that will test against this code and should get me above or at least to the 75% code coverage requirement. Can someone please help me out here?

I am extremely green when it comes to programming and I have been thrust into this role at my new job.

Thanks
Best Answer chosen by Marty Lavender
Gigi.OchoaGigi.Ochoa
Create a new apex test class:
@isTest
private class CustomLoginController_Test {

    static testMethod void testlogin() 
    {     
       
       CustomLoginController controller = new CustomLoginController();     
       
       controller.username = 'username';
       controller.password = 'password';

       controller.login();
       
    }
}



All Answers

Gigi.OchoaGigi.Ochoa
Create a new apex test class:
@isTest
private class CustomLoginController_Test {

    static testMethod void testlogin() 
    {     
       
       CustomLoginController controller = new CustomLoginController();     
       
       controller.username = 'username';
       controller.password = 'password';

       controller.login();
       
    }
}



This was selected as the best answer
Marty LavenderMarty Lavender
Well Im at least up to 70% code coverage now.

Thanks for the help.
Gigi.OchoaGigi.Ochoa
This article is a good intro to writing test methods.  

https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

The test method in my previous post should give you code coverage, but I would receommend adding some assertions as well.
Marty LavenderMarty Lavender
You are referring to System.Assert ?
James LoghryJames Loghry
Yes, Gigi is referring to System.asserts.  Essentially, without System.asserts you're just hacking the code coverage without actually verifying that your code is working properly.  To add a system assert to your class, you would do the following:

@isTest
private class CustomLoginController_Test {
    static testMethod void testlogin(){    
       CustomLoginController controller = new CustomLoginController();    
       controller.username = 'username';
       controller.password = 'password';

        Test.startTest();
        PageReference pageRef = controller.login();
        Test.stopTest();

        //Assert your pageRef is not null
        System.assertNotEquals(null,pageRef);
        //You could be more explicit with your cert if you know the pagename that your Site Login redirects to
        //System.assertEquals(Page.SiteRegister.getUrl(),pageRef.getUrl());
    }
}