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
Giri Kumar BGiri Kumar B 

i am writing class account and contact insert record. same class i write the test class but not code coverage 100% . were i am miss the logic

class
---------------
public class textex2 {
public string lastname ;
public string phone;
    public void create(){
        if(phone == '123'){
            account acc = new account();
            acc.name = lastname;
            acc.Phone = phone;
            insert acc;
        }else{
            contact c = new contact();
            c.lastname = 'test1';
            c.phone = '0223';
            insert c;
        }
      
    }
}
------------------------
test
@istest
public class testclassex2 {
@istest
    static void testdata2(){
        textex2 ts = new textex2();
        ts.lastname = 'test1';
        ts.phone = '125';
        ts.create();
        if(ts.phone == '123'){
        integer count = [select count() from account];
            system.assertEquals(count, 1);
            }
        ts.phone ='0234';
        ts.create();
        if(ts.phone != '123'){
            integer size = [select count() from contact];
            system.assertEquals(size,1);
        }
    }
}
Best Answer chosen by Giri Kumar B
Adam GilchristAdam Gilchrist
@istest
public class testclassex2 {
@istest
    static void testdata2(){
        textex2 ts = new textex2();
        ts.lastname='test';
        ts.phone='12345';
        ts.create();
        ts.phone='123';
        ts.create();
        
        }
    }

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
PLease try below code
test
@istest
public class testclassex2 
{
	@istest
    static void testdata2()
	{
        textex2 ts = new textex2();
        ts.lastname = 'test1';
        ts.phone = '125';
        ts.create();
		//integer ct = [select count() from contact];
        //system.assertEquals(ct, 1);
    }
	
	@istest
    static void testdata3()
	{
        textex2 ts = new textex2();
        ts.lastname = 'test1';
        ts.phone = '123';
        ts.create();
		//integer ct = [select count() from account];
        //system.assertEquals(ct, 1);
    }
	
}

Let us know if this will help you
 
Adam GilchristAdam Gilchrist
@istest
public class testclassex2 {
@istest
    static void testdata2(){
        textex2 ts = new textex2();
        ts.lastname='test';
        ts.phone='12345';
        ts.create();
        ts.phone='123';
        ts.create();
        
        }
    }

 
This was selected as the best answer
Giri Kumar BGiri Kumar B
Thank You . Adam Gilchrist & Amith Chaudhary