[SalesForce] APEX Unit tests hello world

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.

Best Answer

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"');

   }
}
Related Topic