Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I am trying to learn how to do APEX testing but all the tests in the documentation are too complex and I can't find an example of a simple APEX Controller that is a Hello World Test.

I have this APEX Code

public class MessageMaker{
public static string helloMessage() {
    return('You say "Goodbye," I say "Hello"');
    }
}

Should I be testing that the String is the same? Here is my test that doesn't work

    @isTest
public class MessageMakerTest {
    public static testMethod void testMyClass(){

       MessageMaker controller = new MessageMaker();
        String helloMessage = helloMessage();

        System.assertEquals(helloMessage, 'You say "Goodbye," I say "Hello"');

    }
}

I am getting this error.

Method does not exist or incorrect signature: helloMessage()

I have some more advanced APEX code but I want to understand what I need to test for and how before I can do that. Thank you in advance.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

You declared helloMessage() method as static.

In your test class, instead of instantiating MessageMaker just refer to the method as MessageMaker.helloMessage();

@isTest
public class MessageMakerTest {

   public static testMethod void testMyClass(){
       String helloMessage = MessageMaker.helloMessage();

       System.assertEquals(helloMessage, 'You say "Goodbye," I say "Hello"');

   }
}
share|improve this answer
    
100% Code coverage! Thank you Kurusu! –  proteuscanvas Jul 27 at 4:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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