0

This question already has an answer here:

I have the following code that clones an asset and its' children...

public class Cloner {    
    public Asset root_asset { get; set; }
    public Asset cloned_root_asset {get; set; }
    public Asset a_clone {get; set; }
    public Id root_id { get; set; }
    public Id cloned_root_id { get; set; }
    public Id idxx { get; set; }
    public List<Asset> children { get; set; }
    public List<Asset> childrenx { get; set; }
    public Cloner(ApexPages.StandardController controller){
        root_asset = (Asset) controller.getRecord();
        root_id = root_asset.Id;
    }
    public void doClone(Asset a, Id p){
        a_clone = a.clone(false, false, false, false);
        //a_clone.RootAssetId = cloned_root_asset.Id;
        a_clone.ParentId = p;
        insert a_clone;
        p = a.Id;
        childrenx = Database.query('SELECT ' + String.join(new List<String>(Schema.getGlobalDescribe().get('Asset').getDescribe().fields.getMap().keySet()), ',') + ' FROM Asset WHERE ParentId = :p');

        for(Asset b : childrenx){
            doClone(b, a_clone.Id);
        }
    }
    public PageReference cloneAsset(){
        root_asset = Database.query('SELECT ' + String.join(new List<String>(Schema.getGlobalDescribe().get('Asset').getDescribe().fields.getMap().keySet()), ',') + ' FROM Asset WHERE Id = :root_id');
        cloned_root_asset = root_asset.clone(false, false, false, false);
        insert cloned_root_asset;
        children = Database.query('SELECT ' + String.join(new List<String>(Schema.getGlobalDescribe().get('Asset').getDescribe().fields.getMap().keySet()), ',') + ' FROM Asset WHERE ParentId = :root_id');
        for(Asset a : children){
            doClone(a, cloned_root_asset.Id);
        }
        idxx = cloned_root_asset.Id;

        PageReference pageRef = new PageReference('/'+ idxx);
        pageRef.setRedirect(true);
        return pageRef; //Returns to the case page
    }
}

With following visualforce page:

<apex:page standardController="Asset" extensions="Cloner" action="{!cloneAsset}">
    <apex:form >
        <apex:inputHidden value="{!asset.OwnerId}"/>
    </apex:form>
</apex:page>

How do I make a test class for the code?

@isTest
private class testCloner {
    @isTest static void myTest() {
        List<Asset> assets = [Select Id from Asset limit 100];
        for(Asset a : assets){
            //Create a new instance of standard controller
            ApexPages.StandardController sc = new ApexPages.standardController(a);
            Cloner c = new Cloner(sc);
            c.cloneAsset();
        }
    }
}

marked as duplicate by Adrian Larson Apr 2 at 22:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 4
    Welcome to SFSE! Please take the tour. Code coverage questions are commonly closed here (we got enough that a few contributors pulled together and added common unit testing questions to the canonical-qa tag). At the heart of it, almost all unit tests follow the same pattern regardless of the code being tested. 1 Set up your test environment and inputs. 2 Call the code you want to test. 3 Gather the results (the output/changes made by running your code), and make assertions to verify that your code behaves as expected. Cloning records doesn't really have much of an impact there. – Derek F Apr 2 at 19:53
  • If you're stuck on a particular part of writing a unit test (e.g. what results should I use as the expectations for my assertions?), your question stands a better chance if you focus on that part alone. You can edit your question to make it more narrow. – Derek F Apr 2 at 19:53
1

So you're going to want to first create asset objects using a test setup method.

@TestSetup
public static void createTestData(){
    //Generate Asset records 
    //Generate Child Records
    //Insert Records
}

Then the next step is to get those records in the test class, and run your code against them. like you already have. After that you'll want to do some system.assert()'s to ensure that your cloner is actually working. Also use the Test.startTest() and Test.stopTest() methods surrounding your cloner to get a fresh execution context.

  • If you don't mind me asking, what's the best way to generate test asset records? Should I just initiate them with basic default variables that are contextually relevant? – coder42 Apr 2 at 20:03
  • In any case, thank you good sir! You're a scholar and a gentleman. That was the advice I needed to understand testing... – coder42 Apr 2 at 20:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.