+ Start a Discussion
BatmansfdcBatmansfdc 

Can some one help me with writing a test class with this apex class?

public without sharing class CreateContactController {

    @AuraEnabled
    public static Id createRecord (Contact contact){
        System.debug('Inserting contact with:' + contact.FirstName + contact.LastName + contact.Email);
        Id returnID = null;        
        if(contact != null){
      User u = [SELECT Id, Contact.AccountId FROM User WHERE Id = :UserInfo.getUserId()][0];
            contact.AccountId = u.Contact.AccountId;
            insert contact;
            returnId = contact.Id;
        }
        else {
            System.debug('Could not insert.');
        }
        return returnId;
    }

}
fer rfer r
@isTest
public class createContactTest{
	@isTest static void creatContactTest() {
		Account ac= new account();
		ac.name='test';
		insert ac;
		Contact con=new Contact();
		con.firstname='test';
		con.lastname='test';
		con.email='mail@test.net';
		con.AccountId=ac.id;
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = 'mail@test.org', 
                         Username ='test@test.org', 
                         EmailEncodingKey = 'ISO-8859-1',  
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ContactId=con.Id;
                        ); 

        id myid=CreateContactController.createRecord(uu);
        myid=CreateContactController.createRecord(null);
	}
}

remove your first debug or it will not work, also you have to have a contact assosiated to your user
SFDomSFDom
Hi Gowtham,

Please try below code .
 
@isTest
public class CreateContactControllerTest {
	@isTest
    public static void unitTest(){
		Profile sysAdminProfile = [SELECT Id FROM Profile where Name='System Administrator' Limit 1];
	
		UserRole role = [Select Id from UserRole Where Name='your user role name' Limit 1];
	
        User userAdmin = new User(
            Username = System.now().millisecond() + 'test12345@test.com',
            UserRoleId = role.Id,
            ProfileId = sysAdminProfile.Id,
            Alias = 'test123',
            Email = 'test12345@test.com',
            EmailEncodingKey = 'UTF-8',
            LastName = 'Testy',
            CommunityNickname = 'test12345',
            TimeZoneSidKey = 'America/Los_Angeles',
            LocaleSidKey = 'en_US',
            LanguageLocaleKey = 'en_US'
            );
        insert userAdmin;
		
        System.runAs(userAdmin){
            
            Profile portalProfile = [SELECT Id FROM Profile where Name='your community profile name' Limit 1];
	
            Account acc = new Account();
            acc.Name = 'test';
            insert acc;
            
            Contact con = new Contact();
            con.LastName = 'test';
            con.AccountId = acc.id;
            insert con;
            
            User user1 = new User(
			Username = System.now().millisecond() + 'test12346@test.com',
			ContactId = con.Id,
			ProfileId = portalProfile.Id,
			Alias = 'test13',
			Email = 'test12346@test.com',
			EmailEncodingKey = 'UTF-8',
			LastName = 'Test',
			CommunityNickname = 'test123',
			TimeZoneSidKey = 'America/Los_Angeles',
			LocaleSidKey = 'en_US',
			LanguageLocaleKey = 'en_US'
			);
            insert user1;
            System.runAs(user1) {
                    contact otempContact = new contact();
                	otempContact.LastName = 'Test temp';
                	otempContact.Email = 'Test@test.com';
                	CreateContactController.createRecord(otempContact);
            }
        }
    }
}
Hope this will helpful for you.Please mark as best answer if it will help you.

Thanks,
 
BatmansfdcBatmansfdc
Hi i am getting the below error

system.queryexception list has no rows for assignment to sobject in test class
Briana L.Briana L.
If you are getting this error, it means that the SOQL is not returning any results on line 5, 7, or 26 of the unit test code posted by SFDom.

Make sure you have modified the SOQL with the variables relevant to your org.