Hello,
I am trying to complete the unit "Write Positive Tests" in the Module "Advanced Apex Testing". For some reason, when I check my answer to the hands-on challenge, I receive the following message:
Challenge not yet complete in Empathetic Panda Playground
We can't find the method “isHighPriority()” in the “AccountWrapperTests” class.
The problem is that this method is definitely in my "AccountWrapperTests" class. Please help me understand what is going wrong.
Here is the link:
The class I am using for the challenge is presented in a reply to this message.
I'm still getting the same issue in April 2024. Is this an issue with the trailhead module?
Class:
public with sharing class AccountWrapper {
public class AWException extends exception {
}
Account thisAccount;
public AccountWrapper(Account startWith) {
thisAccount = startWith;
}
public decimal getRoundedAvgPriceOfOpps() {
AggregateResult[] ar = [
SELECT AVG(Amount)
FROM Opportunity
WHERE accountId = :thisAccount.Id
WITH USER_MODE
];
Decimal average = (Decimal) ar[0].get('expr0');
Long modulus = Math.mod(Integer.valueOf(average), 1000);
Decimal returnValue = (modulus >= 500)
? (average + 1000) - modulus
: average - modulus;
if (returnValue <= 0) {
throw new AWException('No won Opportunities');
}
return returnValue;
}
public Boolean isHighPriority() {
if (getRoundedAvgPriceOfOpps() > 100000.00) {
return true;
}
return false;
}
}
Tests:
@isTest
public class AccountWrapperTests {
@TestSetup
static void loadTestData() {
// GIVEN
Account acct = new Account(Name = 'ACME');
insert acct;
List<Opportunity> opps = TestFactory.generateOppsForAccount(acct.id, 1000.00, 5);
insert opps;
}
@IsTest
static void testPositiveRoundedAveragePrice() {
// WHEN
Account acct = [SELECT Id FROM Account LIMIT 1];
AccountWrapper acctWrapper = new AccountWrapper(acct);
// THEN
Test.startTest();
Assert.areEqual(
acctWrapper.getRoundedAvgPriceOfOpps(),
1000.00,
'Expected to get 1000.00');
Test.stopTest();
}
@isTest static void testIsHighPriority() {
List<AccountWrapper> accounts = new List<AccountWrapper>();
List<Opportunity> opps = new List<Opportunity>();
for(Opportunity o : [SELECT ID, amount FROM OPPORTUNITY]){
o.amount = 200000.00;
opps.add(o);
}
update opps;
for(Account a : [SELECT ID, Name FROM ACCOUNT]){
accounts.add(new AccountWrapper(a));
}
// sanity check asserting that we have opportunities before executing our tested method.
List<Opportunity> sanityCheckListOfOpps = [SELECT ID FROM Opportunity];
System.assert(sanityCheckListOfOpps.size() > 0, 'You need an opportunity to continue');
Test.startTest();
for(AccountWrapper a : accounts){
System.assertEquals(a.isHighPriority(), true, 'Expected to get true');
}
Test.stopTest();
}
@isTest static void testPositiveRoundedAveragePriceWithHighAverage() {
List<AccountWrapper> accounts = new List<AccountWrapper>();
List<Opportunity> opps = new List<Opportunity>();
// Creating opportunities with a higher average amount
for(Account a : [SELECT Id FROM Account]){
opps.addAll(TestFactory.generateOppsForAccount(a.Id, 2000.00, 5));
}
insert opps;
// Creating AccountWrapper instances for each account
for(Account a : [SELECT ID, Name FROM Account]){
accounts.add(new AccountWrapper(a));
}
// sanity check asserting that we have opportunities before executing our tested method.
List<Opportunity> sanityCheckListOfOpps = [SELECT ID FROM Opportunity];
System.assert(sanityCheckListOfOpps.size() > 0, 'You need an opportunity to continue');
Test.startTest();
// Verifying that the rounded average price is calculated correctly for accounts with high average opportunity amounts
for(AccountWrapper a : accounts){
Decimal roundedAvgPrice = a.getRoundedAvgPriceOfOpps();
// Expected rounded average price for higher average opportunity amounts (2000.00)
System.assertEquals(roundedAvgPrice, 2000.00, 'Expected to get 2000.00');
}
Test.stopTest();
}
}
Coverage is 93%. Error from Trailhead is: " We can't find the expected declarations for the test method “testIsHighPriority” in the AccountWrapperTests class."
This is driving me crazy, anyone have an idea of where I'm going wrong?