You need to sign in to do that
Don't have an account?
Øyvind Borgersen 10
Issues with test class coverage (62%)
Hi,
I have a simple trigger and test class where I need some help to get from the 62% coverage and up to at least 90%. The lines which are not covered are the ones after the 'IF' statement.
Can someone please help me with the remaining test class code?
The trigger is as follows:
trigger contactCreateCommunityUser on Contact (before insert) {
String currentuser = UserInfo.getUserId();
String emailAddress = UserInfo.getUserEmail();
ID contactId = [Select contactid from User where id =: Userinfo.getUserid()].contactId;
List <User> systemAdm = [SELECT Id
FROM User
WHERE Name like '%Systemadministrator%'];
system.debug('Systemadmin ID ' + systemAdm);
If(contactId != null) {
ID AccID = [Select AccountID from Contact where id =: contactid].AccountId;
for(Contact con : trigger.new){
con.OwnerId = systemAdm.get(0).Id;
}
}
}
Test class:
@isTest
private class contactCreateCommunityUserTest {
static testmethod void contactCreateCommunity() {
Account acc = [SELECT Id
FROM Account
Limit 1];
Contact con = new contact();
con.lastName = 'TestLastName';
con.AccountId = acc.Id;
Insert con;
}
}
I have a simple trigger and test class where I need some help to get from the 62% coverage and up to at least 90%. The lines which are not covered are the ones after the 'IF' statement.
Can someone please help me with the remaining test class code?
The trigger is as follows:
trigger contactCreateCommunityUser on Contact (before insert) {
String currentuser = UserInfo.getUserId();
String emailAddress = UserInfo.getUserEmail();
ID contactId = [Select contactid from User where id =: Userinfo.getUserid()].contactId;
List <User> systemAdm = [SELECT Id
FROM User
WHERE Name like '%Systemadministrator%'];
system.debug('Systemadmin ID ' + systemAdm);
If(contactId != null) {
ID AccID = [Select AccountID from Contact where id =: contactid].AccountId;
for(Contact con : trigger.new){
con.OwnerId = systemAdm.get(0).Id;
}
}
}
Test class:
@isTest
private class contactCreateCommunityUserTest {
static testmethod void contactCreateCommunity() {
Account acc = [SELECT Id
FROM Account
Limit 1];
Contact con = new contact();
con.lastName = 'TestLastName';
con.AccountId = acc.Id;
Insert con;
}
}
-greg
You will get 100% coverage
@isTest
private class contactCreateCommunityUserTest {
static testmethod void contactCreateCommunity() {
List<user> userList = new List<user>();
Account portalAccount1 = new Account(
Name = 'TestAccount'
);
Database.insert(portalAccount1);
Contact contact1 = new Contact(
FirstName = 'Test',
Lastname = 'con1',
AccountId = portalAccount1.Id,
Email = 'test@test.com'
);
Database.insert(contact1);
//Create user for the contact
Profile portalProfile = [SELECT Id FROM Profile WHERE Name = 'Partner Community User' Limit 1];
Profile p = [SELECT Id FROM profile WHERE name='System Administrator'];
User user1 = new User(
Username = 'testportal123test@test.com',
ContactId = contact1.Id,
ProfileId = portalProfile.Id,
Alias = 'test123',
Email = 'test12345@test.com',
EmailEncodingKey = 'UTF-8',
LastName = 'Kumar',
CommunityNickname = 'test12345',
TimeZoneSidKey = 'America/Los_Angeles',
LocaleSidKey = 'en_US',
LanguageLocaleKey = 'en_US'
);
User saUser = new User(alias = 'newUser1',
email='accuser@testorg.com',
emailencodingkey='UTF-8', lastname='Systemadministrator',
languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id,
timezonesidkey='America/Los_Angeles',
username='sauser123@testorg.com');
userList.add(user1);
userList.add(saUser);
Database.insert(userList);
system.runAs(user1)
{
Contact contact2 = new Contact(
FirstName = 'Test',
Lastname = 'Con2',
Email = 'test@testt.com'
);
Database.insert(contact2);
}
}
}
Thanks,
Maharajan.C
I get the following error: System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []
Class.contactCreateCommunityUserTest.contactCreateCommunity: line 52, column 1
This referes to the last section where user1 is addding contactvalues.