H**ello,
**
My unit test return this error “System.NullPointerException: Argument cannot be null.Class.TestUpdateAccountCA.test: line 34, column “
I create apex class service AccountCA for to update a custom field ‘chiffre-d’affaire” on Account records based on the total revenue generated from associated Orders, class TestDataFactory for create elements, but I don’t understant why my asserts it’s not good.
Thank you for your help.
I would like a lead that could unlock my code.
@isTest
private class TestUpdateAccountCA {
@isTest static void test() {
// Create test data
Account testAccount = TestDataFactory.createAccount('Account 1');
Product2 testProduct = TestDataFactory.createProduct2('Produit 1');
PricebookEntry testPriceBookEntry = TestDataFactory.createPricebookEntry(testProduct.Id);
Order testOrder = TestDataFactory.createOrder(testAccount.Id);
OrderItem testOrderItem1 = TestDataFactory.createOrderItem(testOrder.Id, testPriceBookEntry.Id, 10, 150);
OrderItem testOrderItem2 = TestDataFactory.createOrderItem(testOrder.Id, testPriceBookEntry.Id, 20, 1000);
Test.startTest();
// Update the order status
testOrder.Status = 'Ordered';
update testOrder;
Test.stopTest();
// Calculate expected total revenue
Decimal expectedRevenue = (testOrderItem1.Quantity * testOrderItem1.UnitPrice) + (testOrderItem2.Quantity * testOrderItem2.UnitPrice);
// ASSERT
Account updatedAccount = [SELECT Id, Chiffre_d_affaire__c FROM Account WHERE Id = :testAccount.Id LIMIT 1];
System.assertEquals(expectedRevenue, updatedAccount.Chiffre_d_affaire__c, 'The account revenue should be updated correctly.');
}
}
public with sharing class TestDataFactory {
public static Account createAccount(String name) {
Account acc = new Account(Name = name);
insert acc;
return acc;
}
public static Account createAccountWithMultipleOrders (Integer numberOfOrders){
Account accs = new Account(Name = 'Test Account 2');
insert accs;
List<Order> orders = new List<Order>();
for (Integer i = 0; i < numberOfOrders; i++) {
Order order = new Order(
AccountId = accs.Id,
Status = 'Draft',
Pricebook2Id = Test.getStandardPricebookId(),
EffectiveDate = Date.today(),
Name = 'Test Order ' + i
);
orders.add(order);
}
// Insère toutes les commandes
insert orders;
return accs;
}
public static Product2 createProduct2(String name) {
Product2 prod = new Product2(
Name = name,
Family = 'Chemise'
);
insert prod;
return prod;
}
public static PricebookEntry createPricebookEntry(Id productId) {
PricebookEntry pbooke = new PricebookEntry(
Pricebook2Id = Test.getStandardPricebookId(),
Product2Id = productId,
UnitPrice = 1020,
IsActive = true
);
insert pbooke;
return pbooke;
}
public static Order createOrder(Id accountId) {
Order testOrder = new Order(
Name = 'Test Order',
AccountId = accountId,
Status = 'Draft',
Pricebook2Id = Test.getStandardPricebookId(),
EffectiveDate = Date.today()
);
insert testOrder;
return testOrder;
}
public static OrderItem createOrderItem(Id orderId, Id pricebookEntryId, Integer quantity, Decimal unitPrice) {
OrderItem orderItem = new OrderItem(
OrderId = orderId,
PricebookEntryId = pricebookEntryId,
Quantity = quantity,
UnitPrice = unitPrice
);
insert orderItem;
return orderItem;
}
}
public with sharing class AccountCAService {
public static void processOrdersUpdateAccount(List<Order> orders) {
Set<Id> accountIds = new Set<Id>();
for (Order ord : orders) {
if (ord.AccountId != null) {
accountIds.add(ord.AccountId);
}
}
if (accountIds.isEmpty()) {
return;
}
Map<Id, Decimal> accountAmount = new Map<Id, Decimal>();
for (AggregateResult ordersSum : [
SELECT AccountId, SUM(TotalAmount) totalRevenue
FROM Order
WHERE Status = 'Ordered' AND AccountId IN :accountIds
GROUP BY AccountId
])
{
accountAmount.put((Id)ordersSum.get('AccountId'), (Decimal)ordersSum.get('totalRevenue'));
}
List<Account> accountsToUpdate = new List<Account>();
for (Id accountId : accountAmount.keySet()) {
Account acc = new Account(Id = accountId);
acc.Chiffre_d_affaire__c = accountAmount.get(accountId);
accountsToUpdate.add(acc);
}
if (!accountsToUpdate.isEmpty()) {
update accountsToUpdate;
}
}
}