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
zen_njzen_nj 

how to write testmethod that can test for when trigger should be failing?

I am writing a trigger that checks to see when a Case is Closed, that there are no Open Child Cases associated with that Case.

I can get it working fine with plain trigger code:

 

trigger CloseCase_whenNoOpenChildCase on Case (after insert, after update) {

Case[] cc = Trigger.new;

for (Case c:cc)
{
if (c.IsClosed)
{

Integer OpenChildCase = 0;
for (Case ChildCase : [select id from case where ParentId = :c.id and (isClosed != true)])
OpenChildCase++;
if (OpenChildCase > 0)
c.addError('You are trying to Close a Parent Case that still has open child case! Parent Case number is '+c.casenumber+' or Parent Case id = '+c.id);
}
}
}
 
But now I want to use apex classes and so I created a CloseParentCase_NoOpenChildCase class with the CloseoutParentCase method and so replaced the trigger code to this:
trigger CloseCase_whenNoOpenChildCase on Case (after insert, after update) {

Case[] cc = Trigger.new;

for (Case c:cc)
{
if (c.IsClosed)
{
CloseParentCase_NoOpenChildCase.CloseoutParentCase(c);
}
}
}
And the class I created is as followed: 
public class CloseParentCase_NoOpenChildCase
{

public static void CloseoutParentCase (Case c)
{
if (c.isClosed)
{


Integer OpenChildCase = 0;
for (Case ChildCase : [select id from case where ParentId = :c.id and (isClosed != true)])
OpenChildCase++;
if (OpenChildCase > 0)
c.addError('You are trying to Close a Parent Case that still has open child case! Parent Case number is '+c.casenumber+' or Parent Case id = '+c.id);
}
}
 
 
 
And I wanted to create testmethods that will show when I try to close out a case that has child case, it will fail.  But the problem is it looks like when I run the test, it will fail because of the addError call within the class, so the running of the test will fail.
 For example, in my testmethod, I would do something like the code below, but the problem is it would fail at the statement:     update parent_case;
and this is because it would hit the logic in the the CloseoutParentCase method which issues the addError call.
 
So what is the proper way to write a test method when the logic of the trigger/class itself is to result in a failure when certain situation occurs? 
 
  static testmethod void testCloseoutParentCase ()
{


Case parent_case = new Case(RecordTypeId = '0120000000002Ld',Status ='New');
insert parent_case; // hit the trigger

// make sure Case is created
System.assert(parent_case.id != null);
System.assertEquals(parent_case.Status,'New');


Case SRchild_case0 = new Case(RecordTypeId = '0120000000002Ld',Status ='New',parentid=parent_case.id);
insert SRchild_case0; // hit the trigger

// make sure Case is created
System.assert(SRchild_case0.id != null);

// double check

Case SRchild_case = [ select Status from case where parentid = :parent_case.id];

// make sure Case is created
System.assert(SRchild_case.id != null);


// Now try to update the parent case to Closed, it should fail since we have an open child case
// and therefore parent case status should still be New

parent_case.Status='Closed';
update parent_case;

System.assertEquals(parent_case.Status,'New');
 

delete SRchild_case0;
delete SRchild_case;
delete parent_case;
}
}
 
 
 
 

 

Best Answer chosen by Admin (Salesforce Developers) 
JeremyKraybillJeremyKraybill

The general pattern to follow for any test that should be testing for failure is

 

Boolean failed = false; try { // your action here, which should fail } catch (Exception e) { failed = true; } System.assert(failed);

Jeremy Kraybill

Austin, TX

 

 

All Answers

JeremyKraybillJeremyKraybill

The general pattern to follow for any test that should be testing for failure is

 

Boolean failed = false; try { // your action here, which should fail } catch (Exception e) { failed = true; } System.assert(failed);

Jeremy Kraybill

Austin, TX

 

 

This was selected as the best answer
aalbertaalbert

When you use the addError method, it will throw DMLException which can be 'caught' in your test method and you can then Assert the proper error message.

 

 

zen_njzen_nj
Thx for all your help.  Now I know what the try/exception code are suppose to be doing. lol