0

I have a unittest which gives the error:

List has no rows for assignment to SObject in tesclass

I am passing the list from the testclass but is seem this isn't getting into the class

Class

    public without sharing class AddController {

    @AuraEnabled
    public static void saveContacts(List<Contact> listContact){
        Insert listContact;
        Map<Id, Contact> contactMap = new Map<Id, Contact>(listContact);
        createUser(contactMap.keySet());
        system.debug('listContact '+ listContact);
        User currentUser = [SELECT Id, Contact.AccountId FROM User WHERE Id =: UserInfo.getUserId() LIMIT 1];
        for(Contact con : listContact){  
         con.AccountId = currentUser.Contact.AccountId;       
        }
        update listContact;
        system.debug('currentUser '+ currentUser); 
        List<Relatie__c> rel= new List<Relatie__c>(); 
        UserRole ur = [SELECT Id FROM UserRole Where name = 'Partner'];
        for(Contact con : listContact){
            Relatie__c rc = new Relatie__c(Account__c = currentUser.contact.AccountId, Contactpersoon__c = con.id ) ;
            system.debug('rc '+ rc);
            rel.add(rc);
        }
        insert rel;  
        system.debug('rel '+ rel);
    }
    @future public static void createUser(Set<Id> contactIds) {
        Profile profileId = [SELECT Id FROM Profile WHERE Name = 'Community User' LIMIT 1];
        UserRole ur = [SELECT Id FROM UserRole Where name = 'Partner user'];
        List<User> uList= new List<User>();
        Contact[] contactList = [SELECT id, firstname, lastname, email FROM Contact WHERE Id = :contactIds];
        for(Contact con: contactList){
            string alias = con.firstName.substring(0,1) + con.lastName.substring(0,1);
            user u = New user(userName = con.firstName+'.'+con.lastName+'@test.nl' ,
                              firstName = con.firstName, 
                              lastName = con.lastName, 
                              alias = alias, 
                              email = con.email, 
                              communityNickName = alias, 
                              timeZoneSidKey = 'Europe/Amsterdam', 
                              LocaleSidKey = 'nl_NL', 
                              EmailEncodingKey = 'ISO-8859-1', 
                              LanguageLocaleKey = 'nl_NL', 
                              ContactID = con.id,
                              ProfileId = profileId.id//,
                              //UserRoleId = ur.id
                             );
            uList.add(u); }
        insert uList;
        system.debug('uList '+uList);
    }
}

Test

 @isTest
private class AddControllerTest{

    static testmethod void setupTestData(){
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User u=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'Europe/Amsterdam', 
                         LocaleSidKey = 'nl_NL', 
                         LanguageLocaleKey = 'nl_NL', 
                         ProfileId = pf.Id
                        );         
        insert u;

         system.debug('u'+u);

        Account acc = new Account(Name='TestAccount');
        insert acc;
         list<Contact> listContact=new list<Contact>();
        for(Integer i=0;i<50;i++){
            contact con=new contact();
            con.LastName = 'Contact'+i;
            con.AccountId = acc.id;
            listContact.add(con);
        }
        insert listContact;  
        system.debug('listContact'+listContact);

        System.Test.startTest();
        Addcontroller.saveContacts(listContact);
        System.AssertNotEquals(Null,[SELECT id FROM Contact]);
        System.test.stopTest();

    }
}

1 Answer 1

2

You are getting exception here

UserRole ur = [SELECT Id FROM UserRole Where name = 'Partner'];

You need to create UserRole in your test class but as you are not using it in your code so you can also comment this line if not needed. IN case if you want to create userRole here is sample code for you.

UserRole r = new UserRole(DeveloperName = 'MyCustomRole', Name = 'My Role');
insert r;
7
  • I left parts of the code out of my question. I updated the data so you can see I the whole code
    – Thomas
    Feb 14, 2018 at 7:52
  • @Thomas Still you need to insert UserRole in your class. Feb 14, 2018 at 8:21
  • I commented out the UserRole but still get the same error
    – Thomas
    Feb 14, 2018 at 8:31
  • @Thomas UserRole is queried in both method. Can you check. Also share the line number. Feb 14, 2018 at 9:08
  • his is the line: ERROR Class.AddControllerTest.setupTestData: line 5, column 1
    – Thomas
    Feb 14, 2018 at 9:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .