Salesforce test code creator

Posted by ExiaHuang on October 27, 2018

Topic

  1. Auto Create test class.
  2. Soql to apex code.

Environment

  • Make sure you can login your sfdc. Test it : SFDC-XY > Login SFDC

Write a test class

Auto Create TestCode

Tips: It just create the code in the localhost. It will not deploy to your sfdc server.

Open the apex code, Right click.

sample code:

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
/**
* @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;
        }
}

Right click, SFDC-XY Code Creator >Create test code

1540449212136

It will be create a test class like this:

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
/**
* @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());
        // }
    }


}

Insert test data from Soql

Insert Test Data(From SOQL)

Input a soql query string.

1
select id,name,comment__c,content__c from Blog__c limit 2

1540449792963

Right click, SFDC-XY Code Creator > Insert Test Data(From Soql)

1540449870938

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 = 'a006F00002pzN2TQAU';    // カスタムオブジェクト ID
        blog0.name = 'ID-0013';   // ブログNo
        // blog0.comment__c = '';    // 評価
        blog0.content__c = 'test 承認プロセステスト';   // 内容
        blogList.add(blog0);
        
        Blog__c blog1 = new Blog__c();
        // blog1.id = 'a006F00002k90XhQAI';    // カスタムオブジェクト ID
        blog1.name = 'ID-0000';   // ブログNo
        blog1.comment__c = '11';   // 評価
        blog1.content__c = 'AHVI0uh0';   // 内容
        blogList.add(blog1);
        
        upsert blogList;

Insert random test data

Right click, SFDC-XY Code Creator > Insert Test Data(All Field) or

SFDC-XY Code Creator > Insert Test Data(Needed Field)

1540457409043

Select your sobject, It will create code like below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
List<Blog__c> blogList = new List<Blog__c>();
for(Integer i=0; i<5; i++){
	Blog__c blog = new Blog__c();
	blog.RecordTypeId = recordtypeidList[i].id;    //レコードタイプ ID
	blog.comment__c = '3VG5bF0' + string.valueof(i) ;    //評価
	blog.comment_status__c = 'approve';    //評価ステータス
	blog.content__c = '6p8psSh' + string.valueof(i) ;    //内容
	blog.excerpt__c = 'YLpQQAw' + string.valueof(i) ;    //概要
	blog.status__c = 'Draft';    //ステータス
	blog.title__c = 'ot9xvaa' + string.valueof(i) ;    //タイトル
	blog.ReadOnlyTest__c = 'ld9IPfB' + string.valueof(i) ;    //ReadOnlyTest
	blog.Account__c = accountList[i].id;    //取引先
	blogList.add(blog);
}
insert blogList;