I am unable to cover the last 3 lines in the following class. Following is my APEX..

public with sharing class redirectOrderPartner {

    public String currentRecordId {get;set;}
    public List<orders__c> orderId {get;set;}

    public redirectOrderPartner(ApexPages.StandardController controller) {

    Public Pagereference go() {

        currentRecordId  = ApexPages.CurrentPage().getparameters().get('orderid');
        //currentRecordId  = order number from SAP
        system.debug('---currentRecordId  ---'+currentRecordId  );

        List<orders__c> orderId = new List <orders__c>();

        orderId = [select id from orders__c where Orders__c =: currentRecordId];

            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Order ID not available!'));
            return null;         
            PageReference pageRef = new PageReference('/apex/mypage?id='+orderId[0].id);
            return pageRef;

Following is my TEST class...

public class redirectOrderPartner_TEST
    static testMethod void Test(){

        Account acc = new Account(Name='yo yo.');
        insert acc;

        orders__c tempOrder = new orders__c(name = '0001234567', account__c = acc.id );
        insert tempOrder;


        PageReference pageRef = Page.redirectOrderPartner;

        ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(tempOrder);
        redirectOrderPartner ec = new redirectOrderPartner(sc);

    static testMethod void Test1() {

        Account acc1 = new Account(Name='1ABC Corp.');
        insert acc1;

        orders__c tempOrder1 = new orders__c(name = '00234567', account__c = acc1.id );


        PageReference pageRef = Page.redirectOrderPartner;

        ApexPages.Standardcontroller sc1 = new ApexPages.Standardcontroller(tempOrder1);
        redirectOrderPartner ec1 = new redirectOrderPartner(sc1);

Thanks in advance.

  • 1
    this SOQL looks odd: orderId = [select id from orders__c where Orders__c =: currentRecordId]; don't you want the where to be id =:currentRecordId ?
    – cropredy
    Feb 17 '15 at 18:15
  • Agree with Crop above. You're querying orders__c by a field called order__c. None of your tests is setting that field. If that is really not a bug then consider setting the order__c field in your tests.
    – mkorman
    Dec 19 '15 at 12:26
  • In addition, you do not have a test class here. All you have is an execution class. You are not testing anything...Just giving you a heads up that if something changes or does not work as expected this test will not alert you to it most likely. You need to add some asserts, and break it up into a more meaningful structure where you test features / requirement both positive and negative in a test method and not the whole process.....Just FYI
    – Eric
    Dec 19 '15 at 15:54

I do not see the difference between 'test' and 'test1', those two test cases are identical. Anyway, in order to cover the last 3 lines, you need a scenario where orderId list is not empty.

  • Note that in both scenarios you're passing the order name to the Visualforce page and not the the order Id. I do not know the implementation of you custom object Order__c, but I guess that the fields Name and Orders__c are different. Feb 17 '15 at 15:24
  • n.b. test1 does not insert any orders__c - it instantiates one but does not insert
    – cropredy
    Feb 17 '15 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.