Hi, this program describes test class for apex class DML operations
Apex Class
public class TestExample3 {
public Account acc{set;get;}
public string Name{set;get;}
public string Industry{set;get;}
public TestExample3(){
acc = new Account();
}
public void create(){
acc.Name=Name;
acc.Industry=Industry;
if(Industry=='Banking'){
acc.Phone='1234';
acc.Ownership='Public';
}else{
acc.Phone='4567';
acc.Ownership='Private';
}
}
}
Test Class
@isTest
private class TestExample3Test {
static testMethod void testme(){
TestExample3 t = new TestExample3();
t.Name='Testing';
t.Industry='Banking';
t.create();
Account acc1=[select Name,Phone,Ownership,Industry from Account where id=:t.acc.Id];
system.assertEquals('1234', acc1.Phone);
system.assertEquals('Public', acc1.Ownership);
system.assertEquals('Testing', acc1.Name);
}
static testMethod void testme1(){
TestExample3 t1 = new TestExample3();
t1.Name='Testing';
t1.Industry='Education';
t1.create();
Account acc2=[select Name,Phone,Ownership,Industry from Account where id=:t1.acc.Id];
system.assertEquals('4567', acc2.Phone);
system.assertEquals('Private', acc2.Ownership);
system.assertEquals('Testing', acc2.Name);
}
}