Create test class using SalesforceXyTools

SalesforceXyTools

Auto Create VF-Controller-DTO-DAO-Code

Hello,I am Exia.huang. Today I will show you how to create testclass quickly by using SalesforceXyTools. And how to create to create test data quickly.

##

  1. Open the apex code, and find the menu below. [SFDC-XY]->[SFDC Code Creator]->[Create Test Code] SOSSOS

    • The class code which I have selected is like below.

      /
      @author huangxy
      /
      public with sharing class BlogController extends SfdcXyController {

    // DTO Bean
    public BlogDto blogDto {get;set;}

    public BlogController() {

    search();
    

    }

    private void search(){

    <span class="pl-smi">String</span> id <span class="pl-k">=</span> <span class="pl-smi">ApexPages</span><span class="pl-k">.</span>currentPage()<span class="pl-k">.</span>getParameters()<span class="pl-k">.</span>get(<span class="pl-s"><span class="pl-pds">'</span>id<span class="pl-pds">'</span></span>);
    <span class="pl-k">if</span>(<span class="pl-smi">String</span><span class="pl-k">.</span>isBlank(id)){
        <span class="pl-c1">this</span><span class="pl-k">.</span>blogDto <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-smi">BlogDto</span>();
    }<span class="pl-k">else</span>{
        <span class="pl-c1">this</span><span class="pl-k">.</span>blogDto <span class="pl-k">=</span> <span class="pl-k">new</span> <span class="pl-smi">BlogDto</span>(<span class="pl-smi">BlogDao</span><span class="pl-k">.</span>getBlogById(id));
    }
    

    }

    /
    upsert Dto
    /
    public PageReference doSave() {

    <span class="pl-smi">Boolean</span> result;
    
    <span class="pl-smi">Savepoint</span> sp <span class="pl-k">=</span> <span class="pl-smi">Database</span><span class="pl-k">.</span>setSavepoint();
    <span class="pl-k">try</span> {
        upsert blogDto<span class="pl-k">.</span>getSobject();
        result <span class="pl-k">=</span> <span class="pl-c1">true</span>;
    } <span class="pl-k">catch</span>(<span class="pl-smi">DMLException</span> e) {
        <span class="pl-smi">Database</span><span class="pl-k">.</span>rollback(sp);
        <span class="pl-smi">System</span><span class="pl-k">.</span>debug(<span class="pl-s"><span class="pl-pds">'</span>saveDto DMLException:<span class="pl-pds">'</span></span> <span class="pl-k">+</span> e<span class="pl-k">.</span>getMessage());
        result <span class="pl-k">=</span> <span class="pl-c1">false</span>;
    } <span class="pl-k">catch</span>(<span class="pl-smi">Exception</span> e) {
        <span class="pl-smi">Database</span><span class="pl-k">.</span>rollback(sp);
        <span class="pl-smi">System</span><span class="pl-k">.</span>debug(<span class="pl-s"><span class="pl-pds">'</span>saveDto Exception:<span class="pl-pds">'</span></span> <span class="pl-k">+</span> e<span class="pl-k">.</span>getMessage());
        result <span class="pl-k">=</span> <span class="pl-c1">false</span>;
    }
    <span class="pl-k">return</span> <span class="pl-c1">null</span>;
    

    }

    /
    Go Next
    /
    public override PageReference doNext() {

    <span class="pl-smi">Boolean</span> result <span class="pl-k">=</span> doCheck();
    setNextMode(result);
    <span class="pl-k">return</span> <span class="pl-c1">null</span>;
    

    }

    /
    Go Back
    /
    public override PageReference doBack() {

    <span class="pl-smi">Boolean</span> result <span class="pl-k">=</span> <span class="pl-c1">true</span>; 
    setBackMode(result);
    <span class="pl-k">return</span> <span class="pl-c1">null</span>;
    

    }

    /
    do Check
    /
    public override Boolean doCheck() {

    <span class="pl-smi">Boolean</span> result <span class="pl-k">=</span> <span class="pl-c1">true</span>;
    
    <span class="pl-k">return</span> result;
    

    }
    }

    • Select [Create Test Code], the test code will be created automatically like below.

      /
      @author huangxy
      /
      @isTest
      private class BlogControllerTest {

/
This is a test method for BlogController
/
static testMethod void test_BlogController() {

<span class="pl-c"><span class="pl-c">//</span> PageReference pageRef = Page.Blog;</span>
<span class="pl-c"><span class="pl-c">//</span> Test.setCurrentPage(pageRef);</span>
<span class="pl-c"><span class="pl-c">//</span> pageRef.getParameters().put('param1', 'param1');</span>

<span class="pl-smi">Test</span><span class="pl-k">.</span>startTest();

BlogController blogController = new BlogController();

<span class="pl-smi">Test</span><span class="pl-k">.</span>stopTest();

<span class="pl-c"><span class="pl-c">//</span> Check</span>
<span class="pl-c"><span class="pl-c">//</span> System.assert(ApexPages.hasMessages());</span>
<span class="pl-c"><span class="pl-c">//</span> for(ApexPages.Message msg : ApexPages.getMessages()) {</span>
<span class="pl-c"><span class="pl-c">//</span>     System.assertEquals('Upload file is NULL', msg.getSummary());</span>
<span class="pl-c"><span class="pl-c">//</span>     System.assertEquals(ApexPages.Severity.ERROR, msg.getSeverity());</span>
<span class="pl-c"><span class="pl-c">//</span> }</span>

}

/
This is a test method for doSave
/
static testMethod void test_doSave() {

<span class="pl-c"><span class="pl-c">//</span> PageReference pageRef = Page.Blog;</span>
<span class="pl-c"><span class="pl-c">//</span> Test.setCurrentPage(pageRef);</span>
<span class="pl-c"><span class="pl-c">//</span> pageRef.getParameters().put('param1', 'param1');</span>

<span class="pl-smi">Test</span><span class="pl-k">.</span>startTest();

BlogController blogController = new BlogController();
PageReference resultDoSave = blogController.doSave();

<span class="pl-smi">Test</span><span class="pl-k">.</span>stopTest();

<span class="pl-c"><span class="pl-c">//</span> Check</span>
<span class="pl-c"><span class="pl-c">//</span> System.assert(ApexPages.hasMessages());</span>
<span class="pl-c"><span class="pl-c">//</span> for(ApexPages.Message msg : ApexPages.getMessages()) {</span>
<span class="pl-c"><span class="pl-c">//</span>     System.assertEquals('Upload file is NULL', msg.getSummary());</span>
<span class="pl-c"><span class="pl-c">//</span>     System.assertEquals(ApexPages.Severity.ERROR, msg.getSeverity());</span>
<span class="pl-c"><span class="pl-c">//</span> }</span>

}

/
This is a test method for doNext
/
static testMethod void test_doNext() {

<span class="pl-c"><span class="pl-c">//</span> PageReference pageRef = Page.Blog;</span>
<span class="pl-c"><span class="pl-c">//</span> Test.setCurrentPage(pageRef);</span>
<span class="pl-c"><span class="pl-c">//</span> pageRef.getParameters().put('param1', 'param1');</span>

<span class="pl-smi">Test</span><span class="pl-k">.</span>startTest();

BlogController blogController = new BlogController();
PageReference resultDoNext = blogController.doNext();

<span class="pl-smi">Test</span><span class="pl-k">.</span>stopTest();

<span class="pl-c"><span class="pl-c">//</span> Check</span>
<span class="pl-c"><span class="pl-c">//</span> System.assert(ApexPages.hasMessages());</span>
<span class="pl-c"><span class="pl-c">//</span> for(ApexPages.Message msg : ApexPages.getMessages()) {</span>
<span class="pl-c"><span class="pl-c">//</span>     System.assertEquals('Upload file is NULL', msg.getSummary());</span>
<span class="pl-c"><span class="pl-c">//</span>     System.assertEquals(ApexPages.Severity.ERROR, msg.getSeverity());</span>
<span class="pl-c"><span class="pl-c">//</span> }</span>

}

/
This is a test method for doBack
/
static testMethod void test_doBack() {

<span class="pl-c"><span class="pl-c">//</span> PageReference pageRef = Page.Blog;</span>
<span class="pl-c"><span class="pl-c">//</span> Test.setCurrentPage(pageRef);</span>
<span class="pl-c"><span class="pl-c">//</span> pageRef.getParameters().put('param1', 'param1');</span>

<span class="pl-smi">Test</span><span class="pl-k">.</span>startTest();

BlogController blogController = new BlogController();
PageReference resultDoBack = blogController.doBack();

<span class="pl-smi">Test</span><span class="pl-k">.</span>stopTest();

<span class="pl-c"><span class="pl-c">//</span> Check</span>
<span class="pl-c"><span class="pl-c">//</span> System.assert(ApexPages.hasMessages());</span>
<span class="pl-c"><span class="pl-c">//</span> for(ApexPages.Message msg : ApexPages.getMessages()) {</span>
<span class="pl-c"><span class="pl-c">//</span>     System.assertEquals('Upload file is NULL', msg.getSummary());</span>
<span class="pl-c"><span class="pl-c">//</span>     System.assertEquals(ApexPages.Severity.ERROR, msg.getSeverity());</span>
<span class="pl-c"><span class="pl-c">//</span> }</span>

}

/
This is a test method for doCheck
/
static testMethod void test_doCheck() {

<span class="pl-c"><span class="pl-c">//</span> PageReference pageRef = Page.Blog;</span>
<span class="pl-c"><span class="pl-c">//</span> Test.setCurrentPage(pageRef);</span>
<span class="pl-c"><span class="pl-c">//</span> pageRef.getParameters().put('param1', 'param1');</span>

<span class="pl-smi">Test</span><span class="pl-k">.</span>startTest();

BlogController blogController = new BlogController();
Boolean resultDoCheck = blogController.doCheck();

<span class="pl-smi">Test</span><span class="pl-k">.</span>stopTest();

<span class="pl-c"><span class="pl-c">//</span> Check</span>
<span class="pl-c"><span class="pl-c">//</span> System.assert(ApexPages.hasMessages());</span>
<span class="pl-c"><span class="pl-c">//</span> for(ApexPages.Message msg : ApexPages.getMessages()) {</span>
<span class="pl-c"><span class="pl-c">//</span>     System.assertEquals('Upload file is NULL', msg.getSummary());</span>
<span class="pl-c"><span class="pl-c">//</span>     System.assertEquals(ApexPages.Severity.ERROR, msg.getSeverity());</span>
<span class="pl-c"><span class="pl-c">//</span> }</span>

}

/
This is a test method for all
/
static testMethod void test_all() {

<span class="pl-c"><span class="pl-c">//</span> PageReference pageRef = Page.Blog;</span>
<span class="pl-c"><span class="pl-c">//</span> Test.setCurrentPage(pageRef);</span>
<span class="pl-c"><span class="pl-c">//</span> pageRef.getParameters().put('param1', 'param1');</span>

<span class="pl-smi">Test</span><span class="pl-k">.</span>startTest();

// test BlogController
BlogController blogController = new BlogController();

// test doSave
PageReference resultDoSave = blogController.doSave();

// test doNext
PageReference resultDoNext = blogController.doNext();

// test doBack
PageReference resultDoBack = blogController.doBack();

// test doCheck
Boolean resultDoCheck = blogController.doCheck();

<span class="pl-smi">Test</span><span class="pl-k">.</span>stopTest();

<span class="pl-c"><span class="pl-c">//</span> Check</span>
<span class="pl-c"><span class="pl-c">//</span> System.assert(ApexPages.hasMessages());</span>
<span class="pl-c"><span class="pl-c">//</span> for(ApexPages.Message msg : ApexPages.getMessages()) {</span>
<span class="pl-c"><span class="pl-c">//</span>     System.assertEquals('Upload file is NULL', msg.getSummary());</span>
<span class="pl-c"><span class="pl-c">//</span>     System.assertEquals(ApexPages.Severity.ERROR, msg.getSeverity());</span>
<span class="pl-c"><span class="pl-c">//</span> }</span>

}

}

  1. Insert Test Data(All Field) I will select Blog__c. SOSSOSSOS

    List<Blogc> blogList = new List<Blogc>();
    for(Integer i=0; i<5; i++){
    Blogc blog = new Blogc();
    blog.commentc = CoaM5Vy + string.valueof(i) ; //評価
    blog.comment_status
    c = spam; //評価ステータス
    blog.contentc = biKtXlF + string.valueof(i) ; //内容
    blog.excerpt
    c = QVbhcE8 + string.valueof(i) ; //概要
    blog.statusc = Draft; //ステータス
    blog.title
    c = 52lF1IP + string.valueof(i) ; //タイトル
    blogList.add(blog);
    }
    insert blogList;

  2. Insert Test Data(From SOQL)SOSSOSSOS


    select id,name,commentc,contentc from Blog__c limit 2

    • Select [Insert Test Data(From SOQL) ] Menu, the sql will be change to apex code like below.

        List<Blogc> blogList = new List<Blogc>();
      Blogc blog0 = new Blogc();
      // blog0.id = ‘a096F00001yVNHWQA4’; // カスタムオブジェクト ID
      blog0.name = ID-0004; // ブログNo
      blog0.commentc = 評価; // 評価
      blog0.content
      c = 評価; // 内容
      blogList.add(blog0);

    Blogc blog1 = new Blogc();
    // blog1.id = ‘a096F00001yVNHRQA4’; // カスタムオブジェクト ID
    blog1.name = ID-0003; // ブログNo
    blog1.commentc = 評価1e; // 評価
    blog1.content
    c = 内容; // 内容
    blogList.add(blog1);

    upsert blogList;