SalesforceXyTools Sublime Test Code Creator

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]

    • The class code which I have selected is like below.
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      98
      99
      100
      101
      102
      103
      104
      105
      106
      107
      108
      109
      110
      111
      112
      113
      114
      115
      116
      117
      118
      119
      120
      121
      122
      123
      124
      125
      126
      127
      128
      129
      130
      131
      132
      133
      134
      135
      136
      137
      138
      139
      140
      141
      142
      143
      144
      145
      146
      147
      148
      149
      150
      151
      152
      153
      154
      155
      156
      157
      158
      159
      160
      161
      162
      163
      164
      165
      166
      167
      168
      169
      170
      171
      172
      173
      174
      175
      176
      177
      178
      179
      180
      181
      182
      183
      184
      185
      186
      187
      188
      189
      190
      191
      192
      193
      194
      195
      196
      197
      198
      199
      200
      201
      202
      203
      204
      205
      206
      207
      208
      209
      210
      211
      212
      213
      214
      215
      216
      217
      218
      219
      220
      221
      222
      223
      224
      225
      226
      227
      228
      229
      230
      231
      232
      233
      234
      235
      236
      237
      238
      239
      240
      241
      242
      243
      244
      245
      246
      247
      248
      249
      /**
      * @author huangxy
      */
      public with sharing class BlogController extends SfdcXyController {
      // DTO Bean
      public BlogDto blogDto {get;set;}
      public BlogController() {
      search();
      }
      private void search(){
      String id = ApexPages.currentPage().getParameters().get('id');
      if(String.isBlank(id)){
      this.blogDto = new BlogDto();
      }else{
      this.blogDto = new BlogDto(BlogDao.getBlogById(id));
      }
      }
      /**
      * upsert Dto
      */
      public PageReference doSave() {
      Boolean result;
      Savepoint sp = Database.setSavepoint();
      try {
      upsert blogDto.getSobject();
      result = true;
      } catch(DMLException e) {
      Database.rollback(sp);
      System.debug('saveDto DMLException:' + e.getMessage());
      result = false;
      } catch(Exception e) {
      Database.rollback(sp);
      System.debug('saveDto Exception:' + e.getMessage());
      result = false;
      }
      return null;
      }
      /**
      * Go Next
      */
      public override PageReference doNext() {
      Boolean result = doCheck();
      setNextMode(result);
      return null;
      }
      /**
      * Go Back
      */
      public override PageReference doBack() {
      Boolean result = true;
      setBackMode(result);
      return null;
      }
      /**
      * do Check
      */
      public override Boolean doCheck() {
      Boolean result = true;
      return result;
      }
      }
      ```
      * Select [Create Test Code], the test code will be created automatically like below.
      ```java
      /**
      * @author huangxy
      */
      @isTest
      private class BlogControllerTest {
      /**
      * This is a test method for BlogController
      */
      static testMethod void test_BlogController() {
      // PageReference pageRef = Page.Blog;
      // Test.setCurrentPage(pageRef);
      // pageRef.getParameters().put('param1', 'param1');
      Test.startTest();
      BlogController blogController = new BlogController();
      Test.stopTest();
      // Check
      // System.assert(ApexPages.hasMessages());
      // for(ApexPages.Message msg : ApexPages.getMessages()) {
      // System.assertEquals('Upload file is NULL', msg.getSummary());
      // System.assertEquals(ApexPages.Severity.ERROR, msg.getSeverity());
      // }
      }
      /**
      * This is a test method for doSave
      */
      static testMethod void test_doSave() {
      // PageReference pageRef = Page.Blog;
      // Test.setCurrentPage(pageRef);
      // pageRef.getParameters().put('param1', 'param1');
      Test.startTest();
      BlogController blogController = new BlogController();
      PageReference resultDoSave = blogController.doSave();
      Test.stopTest();
      // Check
      // System.assert(ApexPages.hasMessages());
      // for(ApexPages.Message msg : ApexPages.getMessages()) {
      // System.assertEquals('Upload file is NULL', msg.getSummary());
      // System.assertEquals(ApexPages.Severity.ERROR, msg.getSeverity());
      // }
      }
      /**
      * This is a test method for doNext
      */
      static testMethod void test_doNext() {
      // PageReference pageRef = Page.Blog;
      // Test.setCurrentPage(pageRef);
      // pageRef.getParameters().put('param1', 'param1');
      Test.startTest();
      BlogController blogController = new BlogController();
      PageReference resultDoNext = blogController.doNext();
      Test.stopTest();
      // Check
      // System.assert(ApexPages.hasMessages());
      // for(ApexPages.Message msg : ApexPages.getMessages()) {
      // System.assertEquals('Upload file is NULL', msg.getSummary());
      // System.assertEquals(ApexPages.Severity.ERROR, msg.getSeverity());
      // }
      }
      /**
      * This is a test method for doBack
      */
      static testMethod void test_doBack() {
      // PageReference pageRef = Page.Blog;
      // Test.setCurrentPage(pageRef);
      // pageRef.getParameters().put('param1', 'param1');
      Test.startTest();
      BlogController blogController = new BlogController();
      PageReference resultDoBack = blogController.doBack();
      Test.stopTest();
      // Check
      // System.assert(ApexPages.hasMessages());
      // for(ApexPages.Message msg : ApexPages.getMessages()) {
      // System.assertEquals('Upload file is NULL', msg.getSummary());
      // System.assertEquals(ApexPages.Severity.ERROR, msg.getSeverity());
      // }
      }
      /**
      * This is a test method for doCheck
      */
      static testMethod void test_doCheck() {
      // PageReference pageRef = Page.Blog;
      // Test.setCurrentPage(pageRef);
      // pageRef.getParameters().put('param1', 'param1');
      Test.startTest();
      BlogController blogController = new BlogController();
      Boolean resultDoCheck = blogController.doCheck();
      Test.stopTest();
      // Check
      // System.assert(ApexPages.hasMessages());
      // for(ApexPages.Message msg : ApexPages.getMessages()) {
      // System.assertEquals('Upload file is NULL', msg.getSummary());
      // System.assertEquals(ApexPages.Severity.ERROR, msg.getSeverity());
      // }
      }
      /**
      * This is a test method for all
      */
      static testMethod void test_all() {
      // PageReference pageRef = Page.Blog;
      // Test.setCurrentPage(pageRef);
      // pageRef.getParameters().put('param1', 'param1');
      Test.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();
      Test.stopTest();
      // Check
      // System.assert(ApexPages.hasMessages());
      // for(ApexPages.Message msg : ApexPages.getMessages()) {
      // System.assertEquals('Upload file is NULL', msg.getSummary());
      // System.assertEquals(ApexPages.Severity.ERROR, msg.getSeverity());
      // }
      }
      }
  2. Insert Test Data(All Field)
    I will select Blog__c.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    List<Blog__c> blogList = new List<Blog__c>();
    for(Integer i=0; i<5; i++){
    Blog__c blog = new Blog__c();
    blog.comment__c = 'CoaM5Vy' + string.valueof(i) ; //評価
    blog.comment_status__c = 'spam'; //評価ステータス
    blog.content__c = 'biKtXlF' + string.valueof(i) ; //内容
    blog.excerpt__c = 'QVbhcE8' + string.valueof(i) ; //概要
    blog.status__c = 'Draft'; //ステータス
    blog.title__c = '52lF1IP' + string.valueof(i) ; //タイトル
    blogList.add(blog);
    }
    insert blogList;
  3. Insert Test Data(From SOQL)

    1
    select id,name,comment__c,content__c from Blog__c limit 2
    • Select [Insert Test Data(From SOQL) ] Menu, the sql will be change to apex code like below.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    List<Blog__c> blogList = new List<Blog__c>();
    Blog__c blog0 = new Blog__c();
    // blog0.id = 'a096F00001yVNHWQA4'; // カスタムオブジェクト ID
    blog0.name = 'ID-0004'; // ブログNo
    blog0.comment__c = '評価'; // 評価
    blog0.content__c = '評価'; // 内容
    blogList.add(blog0);
    Blog__c blog1 = new Blog__c();
    // blog1.id = 'a096F00001yVNHRQA4'; // カスタムオブジェクト ID
    blog1.name = 'ID-0003'; // ブログNo
    blog1.comment__c = '評価1e'; // 評価
    blog1.content__c = '内容'; // 内容
    blogList.add(blog1);
    upsert blogList;