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
Mars Rover 570Mars Rover 570 

how to write test class for ifelse condition in constructor

public class MyClass {
     public MyClass() {
        if(a=='New') {
         //Do Something}
        else if(a=='Old'){
         //Do Something
 }
}
Devanshu soodDevanshu sood
@isTest
public class test_class{
@isTest
public static void tmethod(){
myclass obj=new myclass();
obj.a='new';
}
@isTest
public static void tmethod1(){
myclass obj=new myclass();
obj.a='old';
}
}

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Devanshu sood
Mars Rover 570Mars Rover 570
Thank you Devanshu.. My Actual code is

public class MyClass {
     public MyClass() {
     for(Account a: [Select name from Account]){
        if(a=='New') {
         //Do Something}
        else if(a=='Old'){
         //Do Something
 }
}
}

 
Glyn Anderson 3Glyn Anderson 3
Mars Rover,

Make sure your test class creates at least two accounts.  One or more with the value "New", and one or more with the value "Old".  When the test calls your code, it will query all the accounts and some will execute the "New" case and others will execute the "Old" case.  All of the code will be covered.
Mars Rover 570Mars Rover 570
Thank you.. I got it.
Glyn Anderson 3Glyn Anderson 3
Could you please either: mark one of these posts as the Best Answer OR post your solution and mark it as the Best Answer.  It will help the others in the community to have this question solved.  Thank you!