+ Start a Discussion
LukeJCraigLukeJCraig 

Apex Trigger Testing

Good evening,
Please could someone help me with testing 3 apex triggers I have created in sandbox?

trigger DrawingStatus on MAI__c (before update) {

for(MAI__c drst:trigger.new){

    if(drst.Short_Term__c == 'Yes'){
        drst.Drawing_Status__c = 'Non Drawer';}
    else if(drst.LC_Max_Vol__c == 0){
        drst.Drawing_Status__c = '';}
    else if(drst.Drawer__c == 'Yes'){
        if(drst.Actual_Variance__c < drst.Permitted_Variance__c){
            drst.Drawing_Status__c = 'Downtrader';}
        else {
            drst.Drawing_Status__c = '';}}
    else {
        drst.Drawing_Status__c = '';}


trigger LTDrawingStatus on MAI__c (before update) {

for(MAI__c lt:trigger.new){

    if(lt.LC_Non_Drawer_Type__c == 'LT'){
        lt.Drawing_Status__c = 'Non Drawer';}


trigger PermittedVariance on MAI__c (before update) {

for(MAI__c pervar:trigger.new){

    if (pervar.Avg_Vol__c > 7500) {
        pervar.Permitted_Variance__c = 0.2;}
    else if (pervar.Avg_Vol__c > 2500) {
        pervar.Permitted_Variance__c = 0.3;}
    else if (pervar.Avg_Vol__c > 675) {
        pervar.Permitted_Variance__c = 0.4;}
    else {
        pervar.Permitted_Variance__c = 0.5;}


Many thanks in advance,

Luke
bob_buzzardbob_buzzard
You are likely to get more responses if you have a go at writing the tests yourself and ask for help when you get stuck.
LukeJCraigLukeJCraig
my current attempt at a test class

@isTest
private class TestTriggers {

    static testMethod void myUnitTest() {
        MAI__c s = new MAI__c();
        s.Account_Number__c = '123456';
        s.Name = 'TRIGGER TESTING';
        s.Vol_Wk_1__c = 1000;
        s.Vo__c = 1000;
        s.Vol_Wk_3__c = 1000;
        s.Binary_Wk_1__c = 1;
        s.Binary_Wk_2__c = 1;
        s.Binary_Wk_3__c = 1;
        s.Last_4_Wks_Vol__c = 3000;
        s.Last_Wk_Vol__c = 1000;

        try
        {
            insert s;
        }
        catch(System.DMLException e)
        {
            System.assert(e.getMessage().contains('Record already exist with same email Id'));
        }
    }
}