function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Yoshinori MoriYoshinori Mori 

Run test for try-catch

I've been trying Run-testing using test class as below.

However, one part of program code after "catch" was not tested.

I would like to know how I should write the program in the test class. 

 

@isTest
private class WXS_TEST_ClassA {

    static testMethod void myUnitTest( ) {

*************************************************

*************************************************

WXS_CL_ClassA controller = new WXS_CL_ClassA( );

*************************************************

*************************************************

controller.save( )

*************************************************

*************************************************

}

 

class to be tested is as follows.

Will it  be about how an error is given to the "controller.save( );"?

 

public with sharing class WXS_CL_ClassA {

*************************************************

*************************************************

 public PageReference save()
    {
        try {
            upsert object name;
            init();
            inputflg = false;
        } catch (DmlException de) {
            System.debug(de.getMessage());
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, de.getMessage()));
        }
        return null;
    }

 

 

AhmedPotAhmedPot

Hi,

 

You need to write test scenario which will raise DML exception when you are calling upsert statement.

 

Thanks,

Ahmed

Yoshinori MoriYoshinori Mori

AhmedPot

 

Thank you for the reply.

Please pardon me for asking you primitive question.

I just started programming a week ago.

 

What I need to do is create another class to manupulate DB and create new instance that causes error in the test class?

Could it be possible to teach me briefly the example of the DB access class to cause the exception?

I  only have a vague image.  

 

Regards

Mori

Ankit AroraAnkit Arora

Hi Mori,

 

If you want to generate the exception for your test class then you can try making mandatory field value null for the object on which controller.save is called. Lets suppose "Name" is mandatory field for object on which you are calling save then do something like this :

 

Create another method in your test class which will contain

 

 

WXS_CL_ClassA controller = new WXS_CL_ClassA();
controller.YourObject.Name = null ; or controller.YourObject.Name = '' ;
controller.save() ;

 

 Hope this will resolve your problem , if it still doesn't then please provide some more code snippets.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Yoshinori MoriYoshinori Mori

Dear Ankit Arora

 

Thank you so much for your quick respondind providing me with the example.

This will be very helpful for me.

I'll try.

 

Mori

 

Yoshinori MoriYoshinori Mori

Dear Ankit Arora

 

I'm not sure whether this is related to the modification.

 

I tried run-testing of the test clas several times after the modifitcation, however, failure occurs and the coverage dropped to 0%.

 

According to the error message, message shows 2 methods that belongs to 2 different triggers not written in all associated test classes are not available.

 

I have never encountered the problem.

 

No programs trgger the 2 trigger's methods.

 

I appreciate if you could help with this issue.

 

Mori

Ankit AroraAnkit Arora

Hi Mori,

 

If there is any failure in trigger when you call save from class then you have to see your trigger code. Also am not sure, but you are talking about 2 triggers on same object , then you have to merge two different triggers which calls on same object insert/update/delete. Also I will be happy to help you if you let me know the exact error on running test and some code snippets of triggers/class/testclass.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Yoshinori MoriYoshinori Mori

Dear Ankit

 

Within all related classes including one testclass( the test class calls other temp classes for testing) do not call any triggers. those related classes are different from other many classes other people are working on.

My part doesn't have triggers.

 

There is one more question.

 

You showed me example ***** yourObject.Name*****

'yourObject' actually stands for something?

 

Mori 

Yoshinori MoriYoshinori Mori

Dear Ankit

 

Please let me clarify what I mentioned in the last message.

 

In case somehow "yourObject.Name =" is executed ,

Will it affect organization objects?

 

I would like to know "youObject" is used as some default object nane in Apex.

 

Thank you

Mori

Ankit AroraAnkit Arora

Mori,

 

Any operated in testclass will NOT affect organisation object (Data). Also "yourObject" is the object(like account , case , contact etc) which is inserted/updated in your controller when save is called.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Yoshinori MoriYoshinori Mori

Dear Ankit

 

Thank you so much!!

Sorry for bothering you.

 

I still have a wonder why there was a "Method Not available" error which my part doesn't call

 

Mori