function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Amol DixitAmol Dixit 

Test Record access with different profile, test Assert failing


I am trying to write Test method to test record access with different profiles. I have one profile for which dont have access to payment records, trying to simutate the same, but In test class, getting Access to Payment records. Due to which assert it failing.

Through salesforce UI, I dont get record access with that profile.  Is there anything I am missing? Could you please help me out in this. Sample code below.
User userObj = TestDataUtility.createUsers(1, 'Charity Auctions Administrator', 'testAuctionAdmin', true )[0];
			List<Account> accountList = TestDataUtility.createAccounts(1, true);
					//Insert single Auction Record.
					List<Auction__c> auctionList = TestDataUtility.createAuctions(1,accountList[0].id, true);
					//Create User
					for(Auction__c auctionObj : auctionList){
						auctionObj.Bid_Amount__c = 200;
						auctionObj.High_Bidder__c =;
						auctionObj.Auction_Status__c = Constants.AUCTION_STATUS_ENDED;
					AuctionStatusChangeHandler.isTriggerInvoked = false;
					//Update Auctions so that Auction status changed to "Ended", Payment records gets created here.
					upsert auctionList;
		userObj = TestDataUtility.createUsers(1, 'Charity Auctions Charity', 'testCharityUser', true )[0];
			List<Payment__c> accountPaymentList = [Select (Select Id, name, OwnerId, RecordTypeId, Payment_Amount__c, Account__c, Auction__c, Payment_Status__c, Payment_Date__c From Payments__r) From Account limit 1].Payments__r;
			System.debug('Owner===>' + accountPaymentList[0].OwnerId);
			System.debug('userObj===>' + userObj.Id);
//Here this Assert is failing, I should get 0 records but getting 1 record.
			System.assertEquals(0, accountPaymentList.size());

i am not sure, but for apex to follow data visibility you will need to use with sahring keyword else it would be without sharing and all data will be accessible in apex.