1

So I wrote a Test Class (for Controller) but on running it is throwing an error for a different Apex class(on Quote) that I've not written, my Apex Class:

public with sharing class ctrlname{
     public quote qid{get;set;}

    public ctrlname(ApexPages.StandardController sc) {
       qid =(quote)sc.getRecord();
   }

 public pagereference navigate(){

   Quote q=[Select OpportunityId,Opportunity.E_Rate_Eligible__c from Quote where Id=:qid.id];
   pagereference p;
   if(q.Opportunity.E_Rate_Eligible__c){

       p=new pagereference('http://newsam.mycompany.com/crm-salesforce/proposal/?id=' + q.Id);

    }else{

       p=new pagereference('http://www.google.com');      

     }

    return p;
 }

my test class:

public static testMethod void testmyExtension() {

Quote q1 = new Quote();

insert q1;

 ApexPages.StandardController sc = new ApexPages.standardController(q1);

 ctrlname me = new ctrlname(sc);

}

How can I reference my test class to my Apex

If needed the test class for another Apex class on Quote:

@isTest

public with Sharing class TestBatchForHexCodeInQuote{

 Static Contact con;

 Static Opportunity opp;

 static testMethod void testMethod1(){

createData();

        Batch_UpdateQuoteHiddenFields temp = new Batch_UpdateQuoteHiddenFields ();
        Database.executeBatch(temp);

        list<Quote> updateQuotes = new list<Quote>();
        for(Quote q : [select Id,ContactId,OpportunityId,Opportunity.E_Rate_Eligible__c from Quote]){
            q.ContactId = con.Id;
            updateQuotes.add(q);
        }
        update updateQuotes;
        con.email = null;
        update con;

        con.email = 'test1@test1.com';
        update con;

        opp.StageName = 'Qualification';
        update opp;

    }
    static void createData(){
        Account acc = new Account(Name = 'test');
        insert acc;

        con = new Contact(LastName = 'test',Email = 'test@test.com',AccountId = acc.id);
        insert con;

        opp = new Opportunity(AccountId = acc.Id,Name = 'test',StageName = 'Proposal',CloseDate = date.today());
        insert opp;

        Quote q  = new Quote(Name = 'test',ExpirationDate = date.today(),OpportunityId = opp.Id);
        insert q;

    }
}

Is it possible to update my test class with this ? If so can someone help on this ? Thanks !

closed as unclear what you're asking by BarCotter, Chris Duncombe, dphil, Adrian Larson, Boris Bachovski Jun 12 '15 at 2:11

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Hi Sanjay, I'm not following you. Can you clearly specify the existing conditions in your codebase - two test classes for the Quote object? Then specify the desired end condition - just one merged test class? – Caspar Harmer Jun 11 '15 at 17:05
  • Hi CaspNZ, so I created a controller and its respective test class but when running the test class I'm receiving an error from another Apex class that is pre-existing. So how can I rectify this issue ? Can we merge both the test classes (controller test class that I've written and the existing Test class) ? – Sanjay Jun 11 '15 at 17:10
  • Well This is confusing.. So your test class is not calling ctrlname class's constructor? From which class you are getting the error? Also paste your whole Test Class please. – Santanu Halder Jun 11 '15 at 17:15
  • My Test class: public static testMethod void testmyExtension() { Quote q1 = new Quote(); insert q1; ApexPages.StandardController sc = new ApexPages.standardController(q1); ctrlname me = new ctrlname(sc); } constructor is ctrlname which should call my Apex class ctrlname – Sanjay Jun 11 '15 at 17:22
  • Sanjay, please use updates to the question to clarify or add more information. That will be easier to find and understand for others. – Samuel De Rycke Jun 11 '15 at 19:44
4

By having this line insert q1; in your test class, that will act as if someone actually inserted a quote record, so the way you are creating your test data is what is causing the other test to fail. You do not need to combine your tests but you do need to ensure that when generating test data it doesn't cause any other tests to fail.
Think about how you would manually test this in the UI. What records would need to exist before the Quote record gets created and what fields need to be populated? Then set up your test to introduce that data into the system.

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