2

I am fairly new to making and/or using tests, and I have come to realise how important these are. I have never actually looked into testing but I have heard of the assert methods. I was wondering how I Would come to use these, how they work and what they do.

I'm sorry if this is brief however, it's not exactly a big question. I just want to know how they work, thank you.

In fact just for more information, I have made a trigger that creates a contact and account when a custom member object is inserted. I made a test that I thought worked, however I do not know if this does, is this section of a test valid? Member Trigger:

trigger MemberTrigger on Member__c (after insert) {
List <Contact> conToInsert = new List <Contact>();
List <Account> accToInsert = new List <Account>();

for(Member__c m : Trigger.new) {    
  Contact c = new Contact();
  Account a = new Account();

  c.FirstName = m.First_Name__c;
  c.LastName = m.Last_Name__c;
  c.Email = m.Email__c;
  c.MailingCity = m.City__c;
  c.MailingCountry = m.Country__c;
  c.MailingStreet = m.Street__c;
  c.MailingPostalCode = m.Post_code_ZIP_code__c;
  c.MobilePhone = m.Mobile__c;
  c.Phone = m.Mobile__c;
  c.Description = m.Description__c;
  c.Department = m.Department__c;
  c.Title = m.Title__c;

  a.Name = m.Name;
  a.AnnualRevenue = m.Revenue__c;
  a.BillingCity = m.City__c;
  a.BillingCountry = m.Country__c;
  a.BillingStreet = m.Street__c;
  a.BillingPostalCode = m.Post_code_ZIP_code__c;
  a.Phone = m.Mobile__c;
  a.Description = m.Description__c;
  a.Website = m.Website__c;
  a.Industry = m.Industry__c;

  accToInsert.add(a);
  try {
    insert accToInsert;
    c.AccountId = a.Id;
    conToInsert.add(c);
    insert conToInsert;
  } catch(system.DmlException e) {
      system.debug(e);
    }
  }
}

The test I tried to write:

@isTest
public class MemberTriggerTest {
  @isTest static void setContactDetails() {
  List <Contact> conToInsert = new List <Contact>();

  Member__c m = new Member__c();
  Contact c = new Contact();

  c.FirstName = m.First_Name__c;
  c.LastName = m.Last_Name__c;
  c.Email = m.Email__c;
  c.MailingCity = m.City__c;
  c.MailingCountry = m.Country__c;
  c.MailingStreet = m.Street__c;
  c.MailingPostalCode = m.Post_code_ZIP_code__c;
  c.MobilePhone = m.Mobile__c;
  c.Phone = m.Mobile__c;
  c.Description = m.Description__c;
  c.Department = m.Department__c;
  c.Title = m.Title__c;

  conToInsert.add(c);
  try {
    insert conToInsert;
  } catch(system.DmlException e) {
    system.debug(e);
  }
 }
}
1
2

Like many modern programming languages, we have the "system" library which has three "assertion" methods in it:

  • System.assert
  • System.assertEquals
  • System.assertNotEquals

You can find out more about them in the docs here.

Here is a simple example:

@isTest
private class AceTest {

    @isTest
    private static void aceTestMethod() {

    Integer a = 2;
    Integer b = 2;

    Integer result = a + b;

    System.assertEquals(4, result);

}

Simple as that!

Edit: Ah, so for your test class... what you actually want to do is insert a MEMBER__C object... and then load the contact and assert it has the right fields in.

Something like:

@isTest
public class MemberTriggerTest {

    // Set up the member with all your fields
    Member__c newMember = new Member__c();
    newMember.First_name__c = 'bob';
    newMember.Mobile__c = '07770';

    // insert it - fires trigger
    insert newMember;

    // load the contact (the only contact that should exist)
    Contact resultantContact = [SELECT FirstName, MobilePhone FROM Contact LIMIT 1];

    // check it's fields
    System.assertEquals('bob', resultantContact.FirstName);
    System.assertEquals('07770', resultantContact.MobilePhone);

    // I would now load the account you expect to have too, and assert all it's fields in the same way ...

}

augment that for more fields and you should be off to a start!

7
  • Hello Simon, thank you for this comment which looks very helpful. I was wondering if you could take a look at the test I attempted above to see if that is somewhat valid as well, thank you.
    – user26719
    Dec 29 '15 at 14:25
  • I was just typing it up ;) I might go back and double check and edit that, so give me 2 more minutes, I just wanted to "post" my edit because I knew you had added this comment already! Dec 29 '15 at 14:29
  • 1
    Thank you, even just reading that gave me more of an insight into how the asserts work - thanks for helping :)
    – user26719
    Dec 29 '15 at 14:34
  • 1
    no probs. good luck! and always come back if you have other questions (it's nice to have such clearly written ones on here for once ;) ) Dec 29 '15 at 14:35
  • 1
    There is a third assertion method... assertNotEquals.
    – Adrian Larson
    Dec 29 '15 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy