
Trigger :
trigger oli_multiple_products_before_insert on OpportunityLineItem (before insert) {
Id userProfileId = userinfo.getProfileId();
String userProfileName = [SELECT ID, Name from Profile Where Id = : userProfileId].Name;
if( userProfileName != 'System Administrator' &&
userProfileName !='Custom Marketing Users 10K 25K '&&
userProfileName !='Customer Service User'&&
userProfileName !='Fulfillment User'
) {
for (OpportunityLineItem oli : Trigger.new) {
if (Trigger.isInsert) {
Integer line_Count = [SELECT COUNT()
FROM OpportunityLineItem o
WHERE o.OpportunityId = :oli.OpportunityId
AND o.PriceBookEntryId = :oli.PriceBookEntryId ];
if (line_Count > 0) {
oli.addError('A Product can not be added more than once to the Opportunity.');
}
}
}
}
}
TestClass :
@isTest
private class Oli_multiple_Products_TestClass{
static testmethod void ValidateOlimultipleproducts(){
Date closeDt = Date.Today();
//Find user with Profile = Sales and Service
Profile SalesNService = [Select id from Profile where Name = 'Sales and Service' limit 1];
User u = new User(
Alias = 'standt',
Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8',
LastName='Testing',
LanguageLocaleKey='en_US',
LocaleSidKey='en_US',
ProfileId = SalesNService.Id,
TimeZoneSidKey='America/Los_Angeles',
UserName='standarduser@testorg.com'
);
Account acc = new Account (Name ='TestClassAccountOpportunitySegment', Region__c = 'AM', Persona__C = 'Artisan');
//System.debug('AccountOpportunitySegment before insert of new Account: ' + acc.segment__C);
insert acc;
Opportunity opp = new opportunity (Name ='TestclassAccountOpportunitySegment', AccountId= acc.Id, StageName = 'Prospecting',
CloseDate = closeDt, ForecastCategoryName = 'Pipeline');
insert opp;
OpportunityLineItem ooli = new OpportunityLineItem (Quantity=2, OpportunityId=opp.Id, TotalPrice=10, PriceBookEntryId='01ud0000004YWFqAAO');
insert ooli;
OpportunityLineItem ooli1 = new OpportunityLineItem (Quantity=2, OpportunityId=opp.Id, TotalPrice=10, PriceBookEntryId='01ud0000004YWFzAAO');
insert ooli1;
}
}

Hi Nikkey, I have removed the redundant lines. Check now, and place an appropriate profile name in the query.
ThanksARIf you find the reply useful that solves your problem then please mark it as best answer.@isTest
private class Oli_multiple_Products_TestClass{
static testmethod void ValidateOlimultipleproducts(){
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
User u = new User(Alias = 'standt', Email='salesnsvc123@abcorg.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', UserName='salesnsvc3214@torg.com');
System.runAs(u) {
Date closeDt = Date.Today();
Account acc = new Account (Name ='TestClassAccountOpportunitySegment', Region__c = 'AM', Persona__C = 'Artisan');
//System.debug('AccountOpportunitySegment before insert of new Account: ' + acc.segment__C);
insert acc;
Opportunity opp = new opportunity (Name ='TestclassAccountOpportunitySegment', AccountId= acc.Id, StageName = 'Prospecting',
CloseDate = closeDt, ForecastCategoryName = 'Pipeline');
insert opp;
OpportunityLineItem ooli = new OpportunityLineItem (Quantity=2, OpportunityId=opp.Id, TotalPrice=10, PriceBookEntryId='01ud0000004YWFqAAO');
insert ooli;
OpportunityLineItem ooli1 = new OpportunityLineItem (Quantity=2, OpportunityId=opp.Id, TotalPrice=10, PriceBookEntryId='01ud0000004YWFzAAO');
try{
insert ooli1;
}
catch(Exception e) {
Boolean exceptionThrown = e.getMessage().contains('A Product can not be added more than once to the Opportunity.')? true : false;
System.assertEquals(exceptionThrown, true);
}
}
}
}