You can refer this link An Introduction to Apex Code Test Methods
to start writing test classes.
Below I have tried to show you following things:
How to create setup data.
How to create user for which you will run the test scenarios.
@isTest public class clsOpportunity10_Test{ @testSetup static void prepareControllerClassData() { //retrieve the profile information Map<String,Id> profileIdNameMap = TestUtil.prepareProfileIdNameMap();
//create Executive user. User mbExecutiveUser = TestUtil.createUser('Executive', 'user', profileIdNameMap.get('Executive')); System.runAs(mbExecutiveUser) { createOpportunities(); }
}
// create an user for the test class public static User createUser(String firstName, String lastName, String profileId){ String sRandom = String.ValueOf(Integer.valueOf(math.rint(math.random()*1000000))); User newUser = new User( ProfileId = profileId, Firstname=firstName, Lastname=lastName, Username = firstName + sRandom + '@xyz.com',
Alias = firstName.substring(0,2) + lastName.substring(0,2), Email=firstName + lastName + '@xyz.com', EmailEncodingKey='UTF-8', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', TimeZoneSidKey='America/Chicago' ); return newUser; }public static Map<String,Id> prepareProfileIdNameMap() { if(profileIdNameMap.isEmpty()) { List lstProfile = [SELECT Id, Name FROM Profile WHERE Name IN ()];
for(Profile objProfile:lstProfile) { profileIdNameMap.put(objProfile.Name, objProfile.Id); } } return profileIdNameMap;
}
public static void createOpportunities() { List lstOpportunity = new List(); Opportunity oppty1 = new Opportunity(); oppty1.RecordTypeId= Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Construction').getRecordTypeId(); oppty1.StageName = 'Needs Qualifying'; oppty1.Branch__c = '10 - Western PA'; //assign all the values to be respective attributes lstOpportunity.add(oppty1);
//same way for other stages and branches create opportunity data. //and finally insert the list insert(lstOpportunity);
}
public static void updateOpportunities() { System.runAs(mbExecutiveUser) { //you will retrieve the opportunity record
//update the record //put System.assert() to verify values }
}}