0

I am stuck with the below code that i m trying to cover : This below code is in my controller ,

public String getuserProfileName() {
    v= [select Profile.Name from User where Id = :Userinfo.getUserId()].Profile.Name;
    return v;
}

and below is my test class

public static testMethod void testgetuserProfileName() {
    String v = [SELECT Profile.Name FROM User WHERE id = :UserInfo.getUserId()].Profile.Name; 
    System.assertEquals('System Administrator', v);
}

The above test method passes successfully however gives no coverage. How can I get coverage?

2

Replicating the code in your test will not cover the class you are replicating

You simply need to call the method you are testing:

public static testMethod void testgetuserProfileName() {
    [className] con = New [className]();
    System.assertEquals('System Administrator', con.getuserProfileName());
}

or to shorten as Adrian has suggested:

public static testMethod void testgetuserProfileName() {
    System.assertEquals('System Administrator', New [className]().getuserProfileName());
}

replace [classname] with the actual name of your class

But, you will need to work on your code a bit as it is likely to fail quite often if the user is not assigned the system admin profile.

Maybe something like:

public static testMethod void testgetuserProfileName() {
    Profile p = [Select Name From Profile Where Id = :userinfo.getProfileId()];
    [className] con = New [className]();
    System.assertEquals(p.Name, con.getuserProfileName());
}

again, shortened version:

public static testMethod void testgetuserProfileName() {
    Profile p = [Select Name From Profile Where Id = :userinfo.getProfileId()];
    System.assertEquals(p.Name, New [className]().getuserProfileName());
}
| improve this answer | |
3

You need to call the actual method:

String runningUserProfileName = MyClass.getUserProfileName();

Note that not all users who run this test will have that profile. You should use system.runAs(user) to guarantee behavior:

String profileName = 'System Administrator';

String result;
system.runAs([SELECT Id FROM User WHERE Profile.Name = :profileName])
{
    Test.startTest();
        result = new MyController().getUserProfileName();
    Test.stopTest();
}

system.assertEquals(profileName, result, '<add_your_own_message>');
| improve this answer | |
  • Thank you for your suggestion Adrian . On trying below @isTest(SeeAllData=true) private class EditControllerTest {public static testMethod void testgetuserProfileName() {String result; system.runAs([SELECT Id FROM User WHERE Profile.Name = :profileName]) {Test.startTest();result = EditController.getUserProfileName(); Test.stopTest();}system.assertEquals(profileName, result, 'Done'); }I get the below message Compile Error: Method does not exist or incorrect signature: EditController.getUserProfileName() at line 24 column 20 Can you please suggest. – Natasha Jun 23 '17 at 21:48
  • 1
    @Natasha You can't call an instance method statically, that's the problem there. – Adrian Larson Jun 23 '17 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.