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
Benjamin SchlundBenjamin Schlund 

Short Apex Test

Hi, we're a non profit org with very limited recources. Therfore thanks for any help since we don't have IT personal.
I wrote this follwing Apex-Code which apparentely needs testing before deployment. I guess it's not to hard, but I have no idea how to do it. Thanks!

public class PV_Stundenerfassung {
 
  public Contact getContact() {
    return [select id, name, PV_Soz_Abgeleistete_Stunden__c,
            (select name, PV_SE_Mitarbeiter__c, PV_SE_Datum__c, PV_SE_Stundenanzahl__c, LastModifiedBy.firstname, LastModifiedBy.lastname, LastModifiedDate, PV_SE_Datum_Anzeige__c, PV_SE_Zuletzt_geaendert_Anzeige__c
             from Stundenerfassung__r )
            from Contact where id =
             :System.currentPageReference()
             .getParameters().get('id')];
  }
}
Best Answer chosen by Benjamin Schlund
v varaprasadv varaprasad
@isTest 
public class PV_StundenerfassungTestClass 
{
 static testMethod void getContactTest() 
 {
 Contact testContact = new Contact();
 testContact.LastName='Test Contact' ;
 insert testContact;
 
 Stundenerfassung__c ac = new Stundenerfassung__c();
 ac.name = 'testst';
 // Here add remaining fields

 Test.StartTest(); 
  ApexPages.StandardController sc = new ApexPages.StandardController(testContact);
  PV_Stundenerfassung  testAccPlan = new PV_Stundenerfassung (sc);

  PageReference pageRef = Page.AccountPlan; // Add your VF page Name here
  pageRef.getParameters().put('id', String.valueOf(testContact.Id));
  Test.setCurrentPage(pageRef);

 
 Test.StopTest();
 }
}

Please check once above and add remain fields in contact and  Stundenerfassung__c  check once.

Hope this helps you.
 

All Answers

v varaprasadv varaprasad
Hi Benjamin,

Above code is related to the visualforce page to test above code open VF page and pass id in the URL after page name 
?id='contactrecordid'

User-added image

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
 
Benjamin SchlundBenjamin Schlund
HI Varaprasad,

thanks for your quick answer. The VF page is working and shows the right data. But I don't have any code coverage for deploying to production. For this I thought you have to write some kind of test class, but since there is no if-statement or anything, I don't know what to test for. Also I have no IT background. If you could help me out with the code, I'd be very grateful.
v varaprasadv varaprasad
@isTest 
public class PV_StundenerfassungTestClass 
{
 static testMethod void getContactTest() 
 {
 Contact testContact = new Contact();
 testContact.LastName='Test Contact' ;
 insert testContact;
 
 Stundenerfassung__c ac = new Stundenerfassung__c();
 ac.name = 'testst';
 // Here add remaining fields

 Test.StartTest(); 
  ApexPages.StandardController sc = new ApexPages.StandardController(testContact);
  PV_Stundenerfassung  testAccPlan = new PV_Stundenerfassung (sc);

  PageReference pageRef = Page.AccountPlan; // Add your VF page Name here
  pageRef.getParameters().put('id', String.valueOf(testContact.Id));
  Test.setCurrentPage(pageRef);

 
 Test.StopTest();
 }
}

Please check once above and add remain fields in contact and  Stundenerfassung__c  check once.

Hope this helps you.
 
This was selected as the best answer
v varaprasadv varaprasad
Stundenerfassung__c ac = new Stundenerfassung__c();
ac.name = 'testst'; // Here add remaining fields
Insert ac;
Benjamin SchlundBenjamin Schlund
Perfect! Thanks a lot!