• rraisch
  • NEWBIE
  • 0 Points
  • Member since 2009

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

Hi.

 

Several triggers I've written call class functions that wrap database operations with the goal that calling functions can perform a database rollback if a wrapped operation fails.

 

Since this is my first real attempt at APEX coding and after some head scratching, I've gotten everything covered except two lines I'm not sure how or even if I can test.

 

For example, given a simple wrapper class:

 

 

public class PackageHelper { public class PackageHelperException extends Exception {} public static Boolean addAccountNote(Account a, String title, String body) { if( null == a) throw new PackageHelperException('####addAccountNote: a == null'); System.debug('#################### >> addAccountNote'); title = (null == title || title.length() < 1) ? 'No title provided' : title; body = (null == body || body.length() < 1) ? 'No body provided' : body; try { insert new Note( ParentId = a.Id, Title = title, Body = body); } catch ( System.DmlException e) { System.debug('#################### addAccountNote ' +'could not insert note:'+e.getMessage()); return false; } return true; } // test for addAccountNote private static testMethod void test_addAccountNote() { String title = 'Note Title'; String body = 'Note Body';

Account a;

Note t;

Boolean result;

try { result = addAccountNote((Account)null, title, body); } catch (Exception e) { System.assert(e.getMessage().contains('a == null')); }

System.assertEquals(null, result);

a = new Account(Name='Test Account'); insert a;

 

// add a note

System.assertEquals(true, addAccountNote(a, (String)null, ''));

// was note added?

t = [ select Id, Title, Body from Note where ParentId = :a.Id limit 1 ]; System.assertEquals('No title provided', t.title); System.assertEquals('No body provided', t.body);

 

// ok, delete it delete t;

 

// add another System.assertEquals(true, addAccountNote(a, '', (String)null)); t = [ select Id, Title, Body from Note where ParentId = :a.Id limit 1 ]; System.assertEquals('No title provided', t.title); System.assertEquals('No body provided', t.body); delete t;

 

// and another System.assertEquals(true, addAccountNote(a, title, body)); t = [ select Id, Title, Body from Note where ParentId = :a.Id limit 1 ]; System.assertEquals(title, t.title); System.assertEquals(body, t.body); delete a; } }

 

 

Running the tests on this class reports full coverage except for these two highlighted lines:

 

 

try {
insert new Note( ParentId = a.Id, Title = title, Body = body);
}
catch ( System.DmlException e) {
System.debug('#################### addAccountNote '
+'could not insert note:'+e.getMessage());
return false;
}

 

Perhaps I'm missing something fundamental, but how can I test for an exception which, in the best of all worlds, never occurs and over which I have no control?

 

Thanks.

 

 

Hi.

 

Several triggers I've written call class functions that wrap database operations with the goal that calling functions can perform a database rollback if a wrapped operation fails.

 

Since this is my first real attempt at APEX coding and after some head scratching, I've gotten everything covered except two lines I'm not sure how or even if I can test.

 

For example, given a simple wrapper class:

 

 

public class PackageHelper { public class PackageHelperException extends Exception {} public static Boolean addAccountNote(Account a, String title, String body) { if( null == a) throw new PackageHelperException('####addAccountNote: a == null'); System.debug('#################### >> addAccountNote'); title = (null == title || title.length() < 1) ? 'No title provided' : title; body = (null == body || body.length() < 1) ? 'No body provided' : body; try { insert new Note( ParentId = a.Id, Title = title, Body = body); } catch ( System.DmlException e) { System.debug('#################### addAccountNote ' +'could not insert note:'+e.getMessage()); return false; } return true; } // test for addAccountNote private static testMethod void test_addAccountNote() { String title = 'Note Title'; String body = 'Note Body';

Account a;

Note t;

Boolean result;

try { result = addAccountNote((Account)null, title, body); } catch (Exception e) { System.assert(e.getMessage().contains('a == null')); }

System.assertEquals(null, result);

a = new Account(Name='Test Account'); insert a;

 

// add a note

System.assertEquals(true, addAccountNote(a, (String)null, ''));

// was note added?

t = [ select Id, Title, Body from Note where ParentId = :a.Id limit 1 ]; System.assertEquals('No title provided', t.title); System.assertEquals('No body provided', t.body);

 

// ok, delete it delete t;

 

// add another System.assertEquals(true, addAccountNote(a, '', (String)null)); t = [ select Id, Title, Body from Note where ParentId = :a.Id limit 1 ]; System.assertEquals('No title provided', t.title); System.assertEquals('No body provided', t.body); delete t;

 

// and another System.assertEquals(true, addAccountNote(a, title, body)); t = [ select Id, Title, Body from Note where ParentId = :a.Id limit 1 ]; System.assertEquals(title, t.title); System.assertEquals(body, t.body); delete a; } }

 

 

Running the tests on this class reports full coverage except for these two highlighted lines:

 

 

try {
insert new Note( ParentId = a.Id, Title = title, Body = body);
}
catch ( System.DmlException e) {
System.debug('#################### addAccountNote '
+'could not insert note:'+e.getMessage());
return false;
}

 

Perhaps I'm missing something fundamental, but how can I test for an exception which, in the best of all worlds, never occurs and over which I have no control?

 

Thanks.