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
kavya g 3kavya g 3 

Error: Method must define a body

Hii Floks

I have written a simple test class and getting this error "Method must define a body" @line 4

code:
@istest
 public class testcontactsearch 
{
@istest  public static void testsearchForContacts();
    {
        contactsearch CS= new contactsearch(firstname='kavya');
        system.assertEquals('kavya', c.firstname);
        
    }
}

Thanx in Advance
 
Best Answer chosen by kavya g 3
sfdcMonkey.comsfdcMonkey.com
you got this error because ContactSearch class already create in your org Please change name of your class try this once it's work 
public class ContactSearchingKavya {
public static List<contact> searchForContacts(){
    list<Contact> c = [select id,firstname from contact where firstname = 'kavya' limit 1];
    system.debug('return list ' + c );
    return c ;
  }
}
@isTest 
public class testClass {
    static testMethod void insertNewAccount() {
     List<contact>  cs = ContactSearchingKavya.searchForContacts();      
        for(contact c :cs){
            system.assertEquals('Kavya',c.firstName);
        }
    }
}

Thanks
I hop it helps you :)
 

All Answers

sfdcMonkey.comsfdcMonkey.com
use testMethod keyword in your test class method if it work let me inform and mark it best answer :)
Thanks
sfdcMonkey.comsfdcMonkey.com
hi try this Please
@istest
 public class testcontactsearch 
{
@istest  public static testMethod void testsearchForContacts(){
        contactsearch CS= new contactsearch(firstname='kavya');
        system.assertEquals('kavya', c.firstname);
        
    }
}
Thanks
sfdcMonkey.comsfdcMonkey.com
you get this error because you put a ' ; ' after method name in line number 4
kavya g 3kavya g 3
Hello piyush_soni

testmethod (or) @istest 
we can use any one of these rite.

yup, i removed ; @line 4 but i ended with ''Variable does not exist: c.firstname'' error..

Thank you
sfdcMonkey.comsfdcMonkey.com
@istest
 public class testcontactsearch 
{
@istest  public static testMethod void testsearchForContacts(){
        contact c =  new contact(firstname='kavya');
        system.assertEquals('kavya', c.firstname);
        
    }
}
Thanks Mark it best answer if it helps you :)
kavya g 3kavya g 3
Error is not fixed.
I found new error "duplicate value found: <unknown> duplicates value on record with id: <unknown>"

Thanx for ur support Piyush
 
sfdcMonkey.comsfdcMonkey.com
fine can you share you current test class code Please :)
kavya g 3kavya g 3
Apex code:

public class ContactSearch 
{
public static void searchForContacts()
{
    list<Contact> c=[select id,firstname from contact where firstname='kavya' limit 1];
    system.debug(+c);
}
}

Testclass: (Modified)

@istest
 public class testcontactsearch 
{
@istest  public static void testsearchForContacts()
    {
        
        contact c= new contact(firstname='kavya');
        cs = c.searchForContacts();
        system.assertEquals('kavya', cs.firstname);
        
    }
}
Error: Variable does not exist: cs
 
sfdcMonkey.comsfdcMonkey.com
public class ContactSearch {
public static void searchForContacts(){
    list<Contact> c=[select id,firstname from contact where firstname='kavya' limit 1];
    system.debug('return list ' + c );
}
}
  @istest public class testcontactsearch {  
   @isTest
   public static void testsearchForContacts(){
   testcontactsearch.testsearchForContacts();
     }
   }

now your class is 100% cover :)
 
kavya g 3kavya g 3
not fixed!
error
"duplicate value found: <unknown> duplicates value on record with id: <unknown>"
sfdcMonkey.comsfdcMonkey.com
you got this error because ContactSearch class already create in your org Please change name of your class try this once it's work 
public class ContactSearchingKavya {
public static List<contact> searchForContacts(){
    list<Contact> c = [select id,firstname from contact where firstname = 'kavya' limit 1];
    system.debug('return list ' + c );
    return c ;
  }
}
@isTest 
public class testClass {
    static testMethod void insertNewAccount() {
     List<contact>  cs = ContactSearchingKavya.searchForContacts();      
        for(contact c :cs){
            system.assertEquals('Kavya',c.firstName);
        }
    }
}

Thanks
I hop it helps you :)
 
This was selected as the best answer
kavya g 3kavya g 3
Thank you soo much Piyush.. its working