-2

I am having troubles figuring out how to write the test class for the following method in my class. Any help would be greatly appreciated.

 public Oppty_Search_IntegrationOperation.Oppty_SearchResponse performOpptyHelperSearch(Oppty_Search_IntegrationOperation.Oppty_SearchData sData){
       searchData = sData;
        String query= null;
        staticSearchResponse = new Oppty_Search_IntegrationOperation.Oppty_SearchResponse();
        try{
            staticSearchResponse = performSearch(query, searchData);//Perform Oppty search and do salesRep access validation then get list of Opptys
        }catch(Exception e){
            if(exceptionDescription!=null){
                staticSearchResponse.errorMessage = exceptionDescription;
            }else {
                staticSearchResponse.errorMessage = 'Exception : ' + e.getMessage();
            }
            staticSearchResponse.isSuccess = false;
        }
        System.debug('**** final response = '+ staticSearchResponse);
        return staticSearchResponse;
    }

This is what i currently have in my test class:

@isTest
private class OpportunitySearch_Ali{
  static testMethod void OpportunitySearch_Ali_Svc()
  {
      Oppty_Search_IntegrationOperation.Oppty_SearchData opptySearchData = new Oppty_Search_IntegrationOperation.Oppty_SearchData();
      Oppty_Search_IntegrationOperation.Oppty_SearchResponse opptySearchResponse = new Oppty_Search_IntegrationOperation.Oppty_SearchResponse();
      Oppty_SearchHelper_IntegrationOperation searchHelper = new Oppty_SearchHelper_IntegrationOperation();
      opptySearchData.opptyName= 'test';
      opptySearchData.opptyNo = '123';
      opptySearchData.soldToPartyNo = '1234';
      opptySearchData.endCustPartyNo = '1232';
      opptySearchData.tier2PartyNo = '123';
      opptySearchData.distributionChannel = 'Direct';
      String query = 'SELECT  Name, Opportunity_Number__c,StageName,Amount,Sales_Channel__c,CloseDate,Account.Name,Distribution_VAR__r.Name FROM Opportunity ';
      String setValues = 'Indirect Distribution Channels';


      opptySearchResponse.isSuccess= true;
      opptySearchResponse.errorMessage = 'Exception in Main class';

      Oppty_Search_IntegrationOperation.performOpptySearch( opptySearchData);

      List<Opportunity> oSR = [select Name from Opportunity where Name = 'test'];
      System.assertEquals(oSR.size(), 0);

      Oppty_Search_IntegrationOperation.Oppty_SearchData sData = new Oppty_Search_IntegrationOperation.Oppty_SearchData();
      sData.opptyName= 'test';
      sData.opptyNo = '123';
      sData.soldToPartyNo = '1234';
      sData.endCustPartyNo = '1232';
      sData.tier2PartyNo = '123';
      sData.distributionChannel = 'Direct';
      Oppty_Search_IntegrationOperation.performOpptySearch(opptySearchData);


    //Insert Account records
    List<Account> accList = new List<Account>();
    accList.add(new Account(Name = 'TestAcount0',Party_Number__c='111',ShippingCountry = 'United States'));
    accList.add(new Account(Name = 'TestAcount1',Party_Number__c= '222',ShippingCountry = 'United States'));
    accList.add(new Account(Name = 'TestAcount3',Party_Number__c= '333',ShippingCountry = 'United States'));
    accList.add(new Account(Name = 'TestAcount4',Party_Number__c= '444',ShippingCountry = 'United States'));
    insert accList;


    //Insert Opportunity records
    List<Opportunity> oppList = new List<Opportunity>();
    oppList.add(new Opportunity (Name = 'Test Opportunity ', AccountId = accList[0].id, Sales_Force__c = 'EMC',StageName = 'Pipeline',CloseDate = System.today(),Amount = 500, Opportunity_Number__c = '9999999',Quote_Cart_Number__c = '1000'));
    oppList.add(new Opportunity (Name = 'Test Opportunity 1', AccountId = accList[1].id, Sales_Force__c = 'EMC',StageName = 'Pipeline',CloseDate = System.today(),Amount = 600, Opportunity_Number__c = '8888888',Quote_Cart_Number__c = '1000'));
    oppList.add(new Opportunity (Name = 'Test Opportunity 2', AccountId = accList[2].id, Sales_Force__c = 'EMC',StageName = 'Pipeline',CloseDate = System.today(),Amount = 700, Opportunity_Number__c = '7777777'));
    oppList.add(new Opportunity (Name = 'Test Opportunity 3', AccountId = accList[3].id, Sales_Force__c = 'EMC',StageName = 'Pipeline',CloseDate = System.today(),Amount = 800, Opportunity_Number__c = '6666666',Quote_Cart_Number__c = '3000'));

    insert oppList;
    List<Opportunity> oppListQueried = new List<Opportunity>();
     oppListQueried = [ Select Id,Name,AccountId,Sales_Force__c,StageName,Amount,Opportunity_Number__c,Quote_Cart_Number__c from Opportunity order by Name];

    searchHelper.filterSalesRepOpptys(oppList, opptySearchData); 
    searchHelper.filterUserOpptys(oppList, opptySearchData);
    searchHelper.performOpptyHelperSearch(opptySearchData);
    searchHelper.performSearch(query, opptySearchData);

   }
}
2
  • 1
    Ray, what are you having trouble with ? Commented May 26, 2015 at 10:02
  • With how to write the test class for this. You'll notice that i have searchHelper.performOpptyHelperSearch(opptySearchData); in my test class but that doesn't seem to be covering any of my code as my coverage percentage is staying the same with or without that bit of code.
    – RayRay
    Commented May 26, 2015 at 10:06

1 Answer 1

0

first of all how many code coverage you have at the moment when you run de test? That should show you if you are in the right direction. Another thing you could start using and would be really useful and safe you a lot of lines of code is DataFactory, check it out here:

Test Data

After this and having a great constructed test class, try to acomplish as much code coverage as you can.

In your code would be nice to test also the lines of code when you get an exception, so remember to force 2 exception error, one to test the if condition and the other to test the else.

Not sure if this was the kind of advice you were serching for but i hope this helps you someway :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .