You need to sign in to do that
Don't have an account?
dboonepes
Need help with Testing
I am really new to Apex and would like some direction in testing a class. Could somebody give me some pointers? I have read through the documentation and am having a mental block. The class is below.
public class StageStations { Integer numberOfAssets; Account myaccount {get; private set;} Asset myasset; Decimal hsid; public StageStations(){ myaccount = [select id, name, Contracted_Stations__c from Account where id = :ApexPages.currentPage().getParameters().get('id')]; for( Asset a : [Select Station_ID__c from Asset Where AccountID = :ApexPages.currentPage().getParameters().get('id')]) hsid = ( hsid > a.Station_ID__c ? hsid : a.Station_ID__c); } public Integer getNumberOfAssets(){ Integer noa = [Select Count() from Asset Where AccountID = :ApexPages.currentPage().getParameters().get('id')]; return noa; } public Account getAccount() { return myaccount; } public Asset getAsset() { if(myasset == null) myasset = new asset(); myasset.Station_ID__c = hsid+1; return myasset; } public PageReference cancel() { pageReference accountpage = new ApexPages.StandardController(myaccount).view(); accountpage.setRedirect(true); return accountpage; } public PageReference save() { myasset.AccountID = myaccount.id; myasset.Name = 'PES Station '+myasset.Station_ID__c; insert myasset; if (myaccount.Contracted_stations__c==[Select Count() from Asset Where AccountID = :ApexPages.currentPage().getParameters().get('id')]){ PageReference acctPage = new ApexPages.StandardController(myaccount).view(); acctPage.setRedirect(true); return acctPage; } else { PageReference newAssetPage = new PageReference(ApexPages.currentPage().getURL()); newAssetPage.setRedirect(true); return newAssetPage; } } }
Hi
See this test class
@isTest
private class stage_Test{
static testmethod void stage_Test()
{
stagestations stag =new stagestations();
Integer num1=stag.getNumberofassets();
Account acc= new Account;
acc = stag.getAccount();
Asset as1 = new Asset();
as1 = stag.getAsset();
stag.cancel();
stag.save();
// in ur code satisfy the if conditions so that it will get 100% code
}}
Let me know any issue
I am getting an error.
System.QueryException: List has no rows for assignment to SObject
Class.StageStations.<init>: line 10, column 1 Class.stage_Test.stage_Test: line 5, column 1
just for ease of reading this is these are the lines
myaccount = [select id, name, Contracted_Stations__c from Account
where id = :ApexPages.currentPage().getParameters().get('id')];
and
stagestations stag =new stagestations();
Also, I had to modify
Account acc= new Account;
to
Account acc= new Account();
for it to compile
Hi
Try this
static testmethod void stage_Test()
{
Account md = new Account();
md.Name = 'raju';
md.Active__c='yes';
md.Contracted_Stations__c=123654.00;
insert md;
ApexPages.currentPage().getParameters().put('id',md.Id);
Account myaccount = [select id, name, Contracted_Stations__c from Account
where id = :md.Id];
stagestations stag =new stagestations();
Integer num1=stag.getNumberofassets();
Account acc= new Account();
acc = stag.getAccount();
contact con = new contact();
con.Lastname='rao';
insert con;
Asset as1 = new Asset();
Asset as2 = new Asset();
as2.Station_ID__c = 12546+1;
as2.Name='rajus';
as2.AccountID=md.Id;
insert as2;
system.debug('as2222222222'+as2);
PageReference pageRef = ApexPages.currentPage();
pageRef.setRedirect(true);
stag.cancel();
// in ur code satisfy the if conditions so that it will get 100% code
}
}