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
Sukriti SharmaSukriti Sharma 

Write a test class for an Apex Class

I wrote an Apex Class to create an account. The user is giving the Account Name as input.  am reading the input using JS and passing the parameter for Apex Class. I have to write a Test Class for the same. Please help me with it.
public with sharing class AddAccount {
    @AuraEnabled
    public static Account insertNewAccount(String accountName){
        Account newAccount = new Account();
        newAccount.Name = accountName;
        try{
            insert newAccount;
            return newAccount;
        }
        catch(Exception d){
            return null;
        }
    }
}

 
Best Answer chosen by Sukriti Sharma
CharuDuttCharuDutt
Hii Sukriti
Try Below Code 100%Coverage
@isTest
public class AddAccountTest {
	@isTEst
    public Static void unitTest(){
        AddAccount.insertNewAccount('Test 123');
    }
    @isTEst
    public Static void unitTest2(){
        AddAccount.insertNewAccount('');
    }
}
Please Mark It As Best Answer If It  Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Sukriti
Try Below Code 100%Coverage
@isTest
public class AddAccountTest {
	@isTEst
    public Static void unitTest(){
        AddAccount.insertNewAccount('Test 123');
    }
    @isTEst
    public Static void unitTest2(){
        AddAccount.insertNewAccount('');
    }
}
Please Mark It As Best Answer If It  Helps
Thank You!
This was selected as the best answer
AnkaiahAnkaiah (Salesforce Developers) 
Hi Sukriti,

try with below code.
@isTest
public class AddAccountTest {

Static testmethod void unitTest(){

    string accname = 'test account';
    AddAccount.insertNewAccount(accname);

    }
 Static testmethod void unitTest2(){
        AddAccount.insertNewAccount('');
    }
}

Thanks!!