1

I'm learning programming and now I would like to have 100% code coverage + meaningful test. How can I test line: lstw.add(new wrapper(acc[i].name,acc[i].id,con[i].LastName,con[i].id)); and how can I check that the Id's of Account and Contact are truly returned?

Thanks in advance

My wrapper class:

    public class WR1 {
    List <Account> acc = new List <Account>();
    List <Contact> con = new List <Contact>();
    public List <wrapper> lstw = new List <wrapper>();
    public List <wrapper> getLstwrapperIntString(){
        acc = [select name, id from Account];
        con = [select LastName, id from Contact];
        system.debug(acc.size());
        for (Integer i=0;i<acc.size();i++){
            lstw.add(new wrapper(acc[i].name,acc[i].id,con[i].LastName,con[i].id));
        }
        return lstw;
    }
    public class wrapper{
        public String AName{get;set;}
        public STring AId{get;set;}
        public String CLastName{get;set;}
        public String CId{get;set;}
        public wrapper (String AName, String AId, String CLastName, String CId){
            this.AName=AName;
            this.AId=AId;
            this.CLastName=CLastName;
            this.CId=CId;
        }
    }
}

My test class:

    @isTest
public class WR1test {
    static testMethod void WR1tst(){
        List <Account> acc = new List <Account>();
        List <Contact> con = new List <Contact>();
        acc = [select name, id from Account where id='0010Y000002Si62QAC'];
        con = [select LastName, id from Contact where id='0030Y000001oQp1QAE'];

        String AName;
        String AId;
        String CLastName;
        String CId;

        WR1 testWR1 = new WR1();
        testWR1.getLstwrapperIntString();
        WR1.wrapper testwrapper=new WR1.wrapper(AName,AId,CLastName,CId);
    }
}
1

There are some things I would change about your code.

  1. I would create an account and contact record in your test method so you don't have to rely on the data in your system.

A good idea is to use the @testSetup syntax to create a setup method. You can find more information at the following link: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_testsetup_using.htm

  1. You can use the System.assertEquals(expected, actual, msg) method to check if you are getting the right values from your code. You can find more information about this method from this link: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_system.htm

So something close to the following should be what you are looking for:

@isTest
public class WR1test {

    @testSetup static void setup(){

        Account acc = new Account(name='Test Account');
        insert acc;
        Contact con = new Contact(FirstName='Test', LastName='Contact', AccountId=acc.Id);
        insert con;
    }

    static testMethod void WR1tst(){
          List <Account> acc = new List <Account>();
          List <Contact> con = new List <Contact>();
          acc = [select name, id from Account limit 1];
          con = [select LastName, id from Contact limit 1];

          String AName = acc.Name;
          String AId = acc.Id;
          String CLastName = con.LastName;
          String CId con.Id;

          List<WR1.wrapper> wrapperList = new List<WR1.wrapper>();
          List<WR1.wrapper> expectedList = new List<WR1.wrapper>();
          WR1 testWR1 = new WR1();
          wrapperList = testWR1.getLstwrapperIntString();
          WR1.wrapper testwrapper=new WR1.wrapper(AName,AId,CLastName,CId);
          expectedList.add(testwrapper);
          System.assertEquals(expectedList, wrapperList, 'Got expected result');
     }
}
  • I think this is more suited as a comment rather than an answer. There isn't really much here that helps the OP (original poster) cover more lines, or make the test(s) meaningful (yes, assertions are required, but it would be better if you explained how to apply them to the test(s) OP has provided). – Derek F Apr 21 '17 at 19:20
4

Before working too hard on the tests take a second look at this code and improve it. Weak code with 100% test coverage is still weak code.

List <Account> acc = new List <Account>();
List <Contact> con = new List <Contact>();
public List <wrapper> lstw = new List <wrapper>();
public List <wrapper> getLstwrapperIntString(){
    acc = [select name, id from Account];
    con = [select LastName, id from Contact];
    system.debug(acc.size());
    for (Integer i=0;i<acc.size();i++){
        lstw.add(new wrapper(acc[i].name,acc[i].id,con[i].LastName,con[i].id));
    }
    return lstw;
}

It looks very fragile. It will fail in any org that has more Contacts than Accounts and will fail in any large org because of governor limits.

It is also not good to have methods that return different results - your getLstwrapperIntString method - each time they are called particularly when using get as a name prefix which would usually be assumed to have no side effects.

  • Hi guys,Thank you for pointing the above to me. As I'm still learning, can you point me to a book/pages where proper programming in SF (Apex/VF) is presented. I have read many articles and examples but it seems I'm still far away. Trailhead seems not to provide this knowledge and developer.salesforce.com is to difficult for me yet – Chris_K Apr 23 '17 at 6:39
  • @Chris_K The main thing is to keep reading and accumulating the information - software is a craft that it takes time to get good at. Along the way do read advancedapex.com. – Keith C Apr 23 '17 at 9:15
0

To test this line You need to be sure that in acc list has at least one Account object. To do this You need to prepare some data before using Your wrapper in test class. You need to create some Accounts in first lines of test class and insert them to Database to be able to get them with SOQLs. Objects Ids are generated when You insert objects to DB, and are automaticaly assigneg so:

Account acc = new Account();
System.debug(acc.Id); // null
insert acc;
System.debug(acc.Id); // Ax230000012310 <-exaample Id

Good practice is to use method with @TestSetup annotation where You can create data for tests, this data will be accessible wuth SOQLs in all test methods without losing governor limits (DMLs, inserts), and code duplication.

If You want to test methods see data from sandbox You must use @IsTest(seeAllData='true'), but it is bad practice ussualy.

  • @isTest(seeAllData=true) is almost universally a bad idea. The attitude I take is that if you have to ask the question (should I use seeAllData=true), the answer is no. – Derek F Apr 21 '17 at 19:23
  • Some specific cases needs this annotation. You can find some topics about them on stack. – patryk Apr 21 '17 at 19:32
  • Hence why I qualified my statement with "almost universally a bad idea". For those rare circumstances where it is required, it's still a bad idea...just unavoidable. This question sits firmly in the territory where using seeAllData=true is not required (and thus should not be used). – Derek F Apr 21 '17 at 19:43

Your Answer

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

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