Need help in User object Test class
@isTest
public class Test_MCShopsPdf {
@isTest
Public static void test(){
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;
Id owner_Id;
User managerInfo;
User uu=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 = 'America/Los_Angeles',
LocaleSidKey = 'en_US',
LanguageLocaleKey = 'en_US',
CurrencyIsoCode='USD',
ManagerId='0057F0000039QXUQA2',
ProfileId = pf.Id
);
insert uu;
System.runAs(uu) {
List<OrderItem > lstoplitm= new list<OrderItem>();
Account a = new Account(name ='testac1',BillingCity='BillingAddress',BillingCountry='India',BillingStreet='test BillingStreet',BillingPostalCode='12345',BillingState='Haryana',Type='Reseller',CurrencyIsoCode='USD', Business_Status__c ='ACTIVE');
insert a;
Product2 prod = new Product2(Name = 'Laptop X200',Family = 'Hardware');
insert prod;
Id pricebookId = Test.getStandardPricebookId();//This is available irrespective of the state of SeeAllData.
PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = prod.Id,UnitPrice = 10000, IsActive = true);
insert standardPrice;
//PriceBook2 pb2Standard = [select Id from Pricebook2 where isStandard=true];
Id SpricebookId = Test.getStandardPricebookId();
PricebookEntry pbe = [ SELECT Id,Product2Id,Pricebook2Id,UnitPrice FROM PricebookEntry WHERE Pricebook2Id = :SpricebookId AND isActive=true LIMIT 1 ];
Order o = new Order(name='test opp1', Status='NA', Offer_Status__c ='Validated', AccountId=a.id, CurrencyIsoCode= 'USD',EffectiveDate = Date.today(),Pricebook2Id = SpricebookId, OwnerId = '0057F0000039QXUQA2');
insert o;
owner_Id = o.OwnerId;
OrderItem op=new OrderItem (PriceBookEntryId=pbe.Id,quantity=1,Orderid=o.id,UnitPrice=pbe.UnitPrice );
lstoplitm.add(op);
insert lstoplitm;
Test.startTest();
//Create Controller instance
MC_Shops_PDFController testOrdPlanMCShop = new MC_Shops_PDFController();
testOrdPlanMCShop.getOrders();
testOrdPlanMCShop.getOrderItemList();
testOrdPlanMCShop.getUserInfo();
Test.stopTest();
}
}
}
here is the apex class :
public class MC_Shops_PDFController {
public List<OrderItem> orderItems { get; set; }
public Id orderId;
public Id owner_Id;
public MC_Shops_PDFController(){
orderId = ApexPages.currentPage().getParameters().get('id');
orderItems = new List<OrderItem>();
}
public List<Order> getOrders(){
List<Order> ordObj = [select Id,Project__c,Account.Name,Account.Email__c,OrderNumber,Name,EndDate,Offer_Participants__c,EffectiveDate,OwnerId, Owner.Name from Order where Id = : orderId];
if(ordObj.size()>0){
owner_Id = ordObj[0].OwnerId; //NOT Covering this line
}
return ordObj;
}
public List<OrderItem> getOrderItemList(){
return [SELECT Id, UnitPrice,ListPrice,Additional_Amount__c, Quantity,Material_No__c, OrderId,Product2.Name,Note__c FROM OrderItem WHERE OrderId = : orderId Order by Product2.Name DESC];
}
public List<string> getUserInfo()
{
String managerId = owner_Id;//'0057F0000039QXUQA2';
String toCheckNull = NULL;
User managerInfo;
List<string> mgrInfo = new List<string>();
while(managerId != null)
{
//NOT Covering this line
managerInfo= [Select Id, ManagerId, Manager.Name FROM User WHERE Id = :managerId] ;
managerId = managerInfo.ManagerId;
toCheckNull = managerInfo.Manager.Name;
if(toCheckNull != null && owner_Id !=null) {
//mgrInfo.add(managerInfo.Manager.Name+' / ');
mgrInfo.add(' / '+managerInfo.Manager.Name);
}
}
if(!mgrInfo.isEmpty()){return mgrInfo;}
else return null;
}
}
Thank you so much @Keiji Otsubo it worked for me, I just changed this line
ApexPages.currentPage().getParameters().put('id',
o.Id);