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
ezhil_kezhil_k 

how to write Test class

pPublic CreateAcc(){
   accountID = '';
   account = new account();
 }
 
 Public void JoinAcc(){
  If (accountID.trim()!=''){
  list<account> acclist = new list<account>();
  acclist = [select id,name,BillingCountry,BillingState,Account_Type__c,Account_ID__c from account where Account_ID__c = :accountID];
account =acclist[0];
   
 
 }

hitesh90hitesh90

share your Full class code here..

ezhil_kezhil_k

public  class CreateAccount {
   public string accountID{get;set;}
   public account account{get;set;}
  
 public CreateAcc(){
   accountID = '';
   account = new account();
 }
 
 Public void JoinAcc(){
  If (accountID.trim()!=''){
  list<account> acclist = new list<account>();
  acclist = [select id,name,Account_Type__c,Account_ID__c from account where Account_ID__c = :accountID];
    If (acclist.size()>0)
  { account =acclist[0];
   
  }
  else
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'You have provided the wrong account number'));  
  }
 

vikas88vikas88

Hi,  

 

 

go through this code:

 

@isTest
public class testclassname
{
public static testMethod void testmethodname()
{

Account acc=new Account(name='testaccount',BillingCountry='testcountry',BillingState='teststate',Account_Type__c='testType', Account_ID__c='testid');
insert acc;
yourclassname obj=new yourclassname();
obj.Account_ID__c='testid';
obj.JoinAcc();

}
}

 

If this solves your problem then please mark it as solution. do not forget to give kudos.

hitesh90hitesh90

Hi,

 

Below is your Test class and Apex class.

 

Apex Class:

public  class CreateAccount {
    public string accountID{get;set;}
    public account account{get;set;}    
    public CreateAccount(){
        accountID = '';
        account = new account();
    }    
    Public void JoinAcc(){
        if(accountID.trim()!=''){
            list<account> acclist = new list<account>();
            acclist = [select id,name,Account_Type__c,Account_ID__c from account where Account_ID__c = :accountID];
            If (acclist.size()>0){
                account =acclist[0];            
            }
            else
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'You have provided the wrong account number'));  
        }
    }
}

 

 

 

Test Class:

@istest
public class TestCreateAccount{
    Private Static testmethod void Testaccountdisplylink(){
        Account objAcc = new Account();
        objAcc.Name = 'Test Account';
        insert objAcc;
        
        Account objAcc1 = new Account();
        objAcc1.Name = 'Test Account';        
        insert objAcc1;
        
        objAcc1.Account_ID__c = objAcc.id;
        update objAcc1;
        
        CreateAccount objCreateAccount = new CreateAccount();
        objCreateAccount.accountID = objAcc.id;
        objCreateAccount.JoinAcc();
        
        objCreateAccount.accountID = objAcc1.id;
        objCreateAccount.JoinAcc();
    }
}

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/

ezhil_kezhil_k

Thank you