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
apex_keenapex_keen 

new to testmethods

I've a trigger which runs 'before update' on opportunity. Now  trying to writing test class for that trigger. In testmethod, there are statements: 

 

 Opportunity getOpp = [select name, amount, contract__c from opportunity where id = '00690000005t1d1'];

getOpp.amount =  5000.00;

update getOpp;

 

but while running, getting following error on query line : 

System.QueryException: List has no rows for assignment to SObject.

 

I tried fixing left side to 'Opportunity[] getOpp' , but then , as I was expecting... error transmit to iind line ( in that case IInd line is getOpp[0].amount, gives error like .. 'out of bound' etc  ). This suggest query is not returning any thing ..

 

But point here is,  opportunity with mentioned ID does exist in system; while running the same query in system log console, it works fine!.. question is, why it is failing inside test method ? Do testmethods always need testdata, created inside test class ? 

 

IInd thing : to fix above ...  I also tried to create opportunity within test class


date mydate = date.parse('02/27/2012');
   Opportunity oppContract= new opportunity( name = 'just for testclass', closedate = mydate , stageName ='Qualification',
    contract__c = '80090000000Dp6R' );
    insert oppContract ;


( note :here contract__c is look up field to contract object)  

 

I then get "same" opportunity using soql query  and  try to update it

 But now  trigger which run on update start giving error .It   has statements like these :


  Map<id,contract> MapofCons = new Map<id,contract>([select id, name,sum_total__c from contract where id in :<setofIds>]);

for (opportunity o : trigger.new )

 If( MapofCons.get(o.contract__c).sum_total__c ==null) 



here I  get error  on " IF statement' , like this  :  System.NullPointerException: Attempt to de-reference a null object (on above line). which  shows , it is not able to reference  contract  object....


I'm little confused, how to proceed now ...please help ! 

 

Note : using developer edition for all this ....


 


steve456steve456

for the first error give Limit 1 in the SOQL query

 

for null pointer exception give

If( MapofCons.get(o.contract__c).sum_total__c !=null) 

 

dmchengdmcheng

You should avoid using hardcoded IDs and avoid selecting existing records from sandbox/production in your unit tests.

 

See this article for ways to write effective unit tests:

http://wiki.developerforce.com/page/How_to_Write_Good_Unit_Tests

apex_keenapex_keen

Giving piece of code : which I think will help here :

 

==================================================================

trigger updateContract on Opportunity (before Update) {

 

set<id> contIds = new set<id>();
list<contract> listcon = new list<contract>();
set<id> oppIds = new set<id>();
Double InitialSum ;

For(Opportunity o :trigger.new)
{
contIds.add(o.contract__c);
oppIds.add(o.id);
}

Map<id,contract> MapofCons = new Map<id,contract>([select id, name,sum_total__c from contract where id in :contIds]);
Map<id, opportunity> MapOfOpps = new Map<id,opportunity>([select id,name, amount from opportunity where id in :oppIds]);

For(opportunity o: trigger.new)
{
 If( MapofCons.get(o.contract__c).sum_total__c ==null)

        -------------------------------------------

          -----------------------------------

           ---------------------------------

 

It is the last line (if statement, which is giving error. If I've created opportunity record in the test class, taking care of look up field.. why then error( exact error mentioned before)  is coming up ....????

 

 

 

 

dmchengdmcheng

Post ytour unit test code and use the Code icon in the message window to format it.

craigmhcraigmh

You can no longer use existing data in test methods. You should be setting up data for your test methods inside your test class.

 

See this article:

http://stackoverflow.com/questions/9164986/how-do-i-avoid-standard-price-not-defined-when-unit-testing-an-opportunitylineit