1

I have a small visual force page which displays the oppotunity info. the controller class has a method "GetOppQuery()". In my sandbox i have total 19 opportunities. when i am running the test, the query inside "GetOppQuery()" method is returning zero rows. can some one please have a look? what is wrong with my code?

    public with sharing class BPO1
{
Id currentUser = UserInfo.getUserId();

Double M0_amount = 0;
Double M1A_amount = 0;
Double M2_amount = 0;
Double M3_amount = 0;
Double M4_amount = 0;
Double M5_amount = 0;
Double M6A_amount = 0;
Double M6B_amount = 0;
Double M7_amount = 0;

Double M0_count = 0;
Double M1A_count = 0;
Double M2_count = 0;
Double M3_count = 0;
Double M4_count = 0;
Double M5_count = 0;
Double M6A_count = 0;
Double M6B_count = 0;
Double M7_count = 0;

Double MTD_Wins_amount = 0;
Double MTD_Wins_count  = 0;
Double YTD_Wins_amount = 0;
Double YTD_Wins_count = 0;





public Integer GetOppQuery()      
      {  
       Opportunity[] opps1 = [Select Id, OwnerId, StageName, CloseDate, Amount From Opportunity  ]; 

         for ( Opportunity opp : opps1)  
         {
            if (opp.StageName == 'M0 - New Opportunity')
            {
                M0_amount = M0_amount + opp.Amount;
                M0_count = M0_count + 1;
            }
            if (opp.StageName == 'M1A - Prospect')
            {
                M1A_amount = M1A_amount + opp.Amount;
                M1A_count = M1A_count + 1;
            }
            if (opp.StageName == 'M2 - Advocate')
            {
                M2_amount = M2_amount + opp.Amount;
                M2_count = M2_count + 1;
            }
            if (opp.StageName == 'M3 - Power Advocate')
            {
                M3_amount = M3_amount + opp.Amount;
                M3_count = M3_count + 1;
            }
            if (opp.StageName == 'M4 - Proof Of Capabilities Accepted')
            {
                M4_amount = M4_amount + opp.Amount;
                M4_count = M4_count + 1;
            }
            if (opp.StageName == 'M5 - Cost Accepted')
            {
                M5_amount = M5_amount + opp.Amount;
                M5_count = M5_count + 1;
            }
            if (opp.StageName == 'M6A - Verbal Approval')
            {
                M6A_amount = M6A_amount + opp.Amount;
                M6A_count = M6A_count + 1;
            }   
            if (opp.StageName == 'M6B - Stalled')
            {
                M6B_amount = M6B_amount + opp.Amount;
                M6B_count = M6B_count + 1;
            }
            if (opp.StageName == 'M7 - Closed Won')
            {
                if (opp.CloseDate.month() == system.today().month())
                {
                    MTD_Wins_amount = MTD_Wins_amount + opp.Amount;
                    MTD_Wins_count = MTD_Wins_count + 1;
                }
                M7_amount = M7_amount + opp.Amount;
                M7_count = M7_count + 1;
            }                   
         }
         return opps1.size();
    }

}

====test class here====

    @isTest
private class BPO1Test {

    static testMethod void myUnitTest() 
    {
        // TO DO: implement unit test

        BPO1 theBPO = new BPO1();
        Integer i = theBPO.GetOppQuery();

        System.debug('getOppSize ='+ i);
        //System.assertEquals(theBPO.getOppSize() , 18);

    }
}

=====debug log=====

13:46:17:010 SOQL_EXECUTE_BEGIN [36]|Aggregations:0|SELECT Id, OwnerId, StageName, CloseDate, Amount FROM Opportunity

13:46:17:012 SOQL_EXECUTE_END [36]|Rows:0

----

13:46:17:000 LIMIT_USAGE_FOR_NS Number of SOQL queries: 1 out of 100

13:46:17:000 LIMIT_USAGE_FOR_NS Number of query rows: 0 out of 50000

can someone please help me? what is wrong with my query?

0

1 Answer 1

2

You have to create your own data when running tests. Something like:

Opportunity record = new Opportunity(/*required fields*/);
insert record;

Then your query will work. See also:

  • Isolation of Test Data from Organization Data in Unit Tests

    Starting with Apex code saved using Salesforce API version 24.0 and later, test methods don’t have access by default to pre-existing data in the organization, such as standard objects, custom objects, and custom settings data, and can only access data that they create. However, objects that are used to manage your organization or metadata objects can still be accessed in your tests such as:

    • User
    • Profile
    • Organization
    • AsyncApexJob
    • CronTrigger
    • RecordType
    • ApexClass
    • ApexTrigger
    • ApexComponent
    • ApexPage
  • Creating Test Data for Apex Tests
  • Getting Started with Apex Unit Tests

    By default, Apex tests don’t have access to pre-existing data in the org, except for access to setup and metadata objects, such as the User or Profile objects. Set up test data for your tests. Creating test data makes your tests more robust and prevents failures that are caused by missing or changed data in the org. You can create test data directly in your test method, or by using a utility test class as you’ll find out later.

  • An Introduction to Apex Code Test Methods

You should also note that without any assertions, you are simply smoke testing. Note from How to Write Good Unit Tests (emphasis mine):

Verify the results are correct

Verifying that your code works as you expect it to work is the most important part of unit testing. It’s also one of the things that Force.com developers commonly neglect. Unit tests that do not verify the results of the code aren’t true unit tests. They are commonly referred to as smoke tests, which aren’t nearly as effective or informative as true unit tests. A good way to tell if unit tests are properly verifying results is to look for liberal use of the System.assert() methods. If there aren’t any System.assert() method calls, then the tests aren’t verifying results properly. And, no, System.assert(true); doesn’t count.

3
  • when i replaced the opportunity query with user object query, the query returned rows. user is a standard object.
    – Anil Sahoo
    Oct 20, 2016 at 22:23
  • @AnilSahoo I see you did not click through to read the articles yourself.
    – Adrian Larson
    Oct 21, 2016 at 3:16
  • yes.. im new to stackexchange.. thanks a lot for ur help..
    – Anil Sahoo
    Oct 24, 2016 at 20:30

You must log in to answer this question.

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