Tuesday, May 20, 2014

Get SOSL covered during Apex Test Execution.

Why my SOSL returning no data during testing??
I created sample data, I saw it in log.I swear but why it return no data?


The hair drop one by one...the wrinkle become finer and finer.She stay quietly facing her laptop.She did not notice that her age is multiplying hastily..


Stop all the drama, let see the point here.The possibility why SOSL not returning data during Apex Test Execution is because Salesforce designed it that way!


Yes, to ensure test class run in predictable way they decide to not return data when you use SOSL.
To ensure that test methods always behave in a predictable way, any Salesforce Object Search Language (SOSL) query that is added to an Apex test method returns an empty set of search results when the test method executes. If you do not want the query to return an empty list of results, you can use the Test.setFixedSearchResults system method to define a list of record IDs that are returned by the search. -more info.
Okay, so we can use this Test.setFixedSearchResults.I gave example how to use it like below.Note that it only show how we can test SOSL NOT how to implement SOSL.
private class MyControllerTest{ 
    private static Account acc1;
   //set up data for each test method
    static {
      acc1 = new Account(Name='MyFatherCompany',Description__c='Produce stuff');
      insert acc1;
      System.assertEquals(1,[select count() from Account  where Name = 'MyFatherCompany' ]);
    static testMethod void test1(){
            //Apply the setFixedSearchResults here
             Id [] fixedSearchResults= new Id[1];
             fixedSearchResults[0] =acc1.Id;
              MyController obj = new MyController();
             obj.searchUsingSOSL(); //This code is contain SOSL.

Now you will see the data, and the method that process those data should be covered and you should live happily ever after.=)

Making the private access modifier visible during test execution.

Whenever I got error  like below during test execution, I will go directly to the class and change the access modifier from private to public.It is not proper because each access modifier serve different purpose, so it is not quite acceptable to modify it just to get PASS for testing.Seem unacceptable but I did it a lot of times. *Evil smile*

But I accidentally find proper solution for this.I posted it in my company site, but I m thinking to share it here.We can use @TestVisible ... @TestVisible everyone.I don't  notice since when it available, but I just noticed it yesterday.Shame!

Well, let's start experiment it.I am using Force.com free edition sandbox to test this.You can find this free sandbox here.

This is my class, notice the private stuff.Private variable and method.

public class MyClass{
    // Private member variable
    @TestVisible private static Integer recordNumber = 1;

    // Private method
    @TestVisible private static void updateRecord(String name) {
       recordNumber =recordNumber +1;

Here the test class.

private class MyClassTest {
    @isTest static void test1() {
        // Access private variable annotated with TestVisible
        Integer i = MyClass.recordNumber;
        System.assertEquals(1, i);

        // Access private method annotated with TestVisible
        // Perform some verification

If you remove @TestVisible, and run the test class you will get the above error.So now instead of changing the access modifier, bear in mind to put @TestVisible whenever you have private stuff to be tested.

Do not reveal your secret just because you want to pass the exam!Keep some mysterious remain.

Please dig more info about @TestVisible here. Have a very nice day ahead. =) .

Friday, May 09, 2014

How to save email thread in GMail into PDF?

This is very easy.I found it when I try to print email thread.This function is there since very long time but I just recognize it today.(-_-)Y

Use the Print icon at the top of the email to print/save as .pdf the whole email thread.If you want to print one email, then use the Print in the sub menu.

Okey bye.Happy Friday.

Wednesday, May 07, 2014

Why we need to use XmlStreamWriter?

Sometimes we [maybe only me] choose the simple method to achieve something.Well, if the objective is done, what else we need to think.

Actually I just want to write about XMLStreamWriter.Some code that I reviewed using simple method to generate XML, which is by appending the string.Well thinking that I going to be safe,I just proceed appending the string like example below.
 String xmlStr ='<user xmlns:i="http://www.w3.org/2001/XMLSchema-instance">';
It goes well after quite some time until one day ,a user shout out about an error. 500 : There was an error deserializing the object of type bla bla.. Whenever a programmer face error, we will google it =) .I notice there & symbol in the data and it caused the issue.So the proper way to write xml is by using XmlStreamWriter.
XmlStreamWriter  w= new XmlStreamWriter();
 w.writeStartElement(null, 'User',null );
        w.writeEndElement(); //groups
        system.debug('@Xml string '+ w.getXmlString());
Okay, bring it on user whatever data you like =) . The example of output
<user xmlns:i="http://unid-machine.blogspot.com">