Question

I have written a trigger for unique asset name.I have to write a test case for it can someone please help I am new to apex.

trigger NameTrigger on Asset (before insert)

{

list al = new List ();

al = [select Name from Asset];

if(Trigger.isInsert)

{

 For(integer i=0;i<al.size();i++)

 {

   for(Asset a2 : Trigger.New)

     {

       if(al[i].Name == a2.Name)

         {

           a2.Name.addError('This value already Exist');

         }

     }

 }

}

}

No correct solution

OTHER TIPS

A test class for your trigger would look something like this.

@isTest
private class AssetTriggerTest {

    private static string assetName = 'TestAsset';
    private static Id accId;

    private static void createAccount(){
        Account acc = new Account(name='TestAcc');
        insert acc;
        accId = acc.Id;
    }

    private static Asset createAsset(){
    if(accId == null)
            createAccount();
        return new Asset(Name=assetName, AccountId=accId);
    }

    private static testMethod void testSingleInsert(){
        insert createAsset();

        List<Asset> assets = [SELECT Id FROM Asset WHERE Name = :assetName];
        system.assertEquals(1, assets.size());
    }

    private static testMethod void testInsertExistingName(){
        insert createAsset();

        Exception e;
        try{
            insert createAsset();
        } catch(Exception ex){
            e = ex;
        }

        system.assertNotEquals(null, e);
        system.assert(e instanceOf system.Dmlexception);
        system.assert(e.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION'));
        system.assert(e.getMessage().contains('This name is already used'));
    }

    private static testMethod void testDoubleInsertSameName(){

        Exception e;
        try{
            insert new List<Asset>{createAsset(), createAsset()};
        } catch(Exception ex){
            e = ex;
        }

        system.assertNotEquals(null, e);
        system.assert(e instanceOf system.Dmlexception);
        system.assert(e.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION'));
        system.assert(e.getMessage().contains('This name is already used'));

    }

}

The class test that the trigger doesn't stop valid data being saved as well as ensuring it stops duplicates being created. I added a third test as well which looks to make sure data isn't saved if there are duplicates within the Trigger.new collection as well. Your trigger currently doesn't cater for this so I have included a copy of how I would right this trigger to pass all three tests; feel free to ignore.

trigger AssetTrigger on Asset (before insert) {

    Map<string, Asset> newAssets = new Map<string, Asset>();
    for(Asset a : Trigger.new){
        if(!newAssets.containsKey(a.Name))
            newAssets.put(a.Name, a);
        else
            a.Name.addError('This name is already used');
    }

    for(Asset a : [SELECT Name FROM Asset WHERE Name IN :newAssets.keySet()]){
        newAssets.get(a.Name).Name.addError('This name is already used');
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top