1

I wrote a very short trigger, nevertheless, I cannot deploy without a test class, but I am not familiar with how to write one. Can you help me write a test class for this trigger?

trigger OppIDtoLookup on ONB2__Invoice__c (before insert, before update) {
  for (ONB2__Invoice__c a : trigger.new) {
     if(a.OppID__c != null){
       a.Opportunity__c = a.OppID__c ;
     }  
  } 
}

thx to all, i read the doc and done some developement...please see the following

   @IsTest
public class TESTONB2Invoice {
static testMethod void TESTONB2Invoice_trigger()
{
    Account a = new Account(Name = 'testAccount',
                            BillingPostalCode = '83059',
                           BillingCountry = 'Deutschland',
                           Genaue_Anzahl_Mitarbeiter__c = 3,
                           Anzahl_Mitarbeiter__c = '0-5',
                           z_Region_WV__c = 'Hamburg');
    insert a; 

    ONB2__Template__c  b = new ONB2__Template__c (Name = 'testTemplate');
        insert b;

    ONB2__InvoiceCounter__c  c = new ONB2__InvoiceCounter__c (Name = 'Default');
        insert c;
        Pricebook2 d = new Pricebook2 (Name = 'Standard');
    insert d;
    Product2 e =new Product2 (Name = 'Top AG');
    insert e;
            Pricebookentry f = new Pricebookentry (Pricebook2Id = '01sD0000000hbEh',
                                          Product2Id = e.Id,
                                          UnitPrice = 250,
                                          usestandardprice = false,
                                          isactive = true);
    insert f;
     Opportunity g = new Opportunity (Name = 'testOpp',
                                      Account = a,
                                     Amount = 250,
                                      Override_Region__c = 'Hamburg',
                                      Pricebook2 = d,
                                     StageName = '40 - Demo Termin vereinbart',
                                     CloseDate = date.today(),
                                     z_Vertragsbeginn__c = date.today());
        insert g;
    OpportunityLineItem h = new OpportunityLineItem ( OpportunityId = g.Id,
                                                     Quantity = 1,
                                                     TotalPrice = 250,
                                                        PricebookentryId = f.Id);
    insert h;
         ONB2__Invoice__c invoice = new ONB2__Invoice__c(ONB2__Account__c = a.id, 
                                                        ONB2__Template__c = b.id, 
                                                        ONB2__Status__c = 'Draft', 
                                                        OppID__c = g.Id); 
    insert invoice;
invoice = [SELECT OppId__c, Opportunity__c FROM ONB2__Invoice__c WHERE Id = :invoice.Id];
System.AssertEquals(invoice.Opportunity__c, invoice.OppId__c);
}
}
3
  • 1
    Please: developer.salesforce.com/page/… Commented Dec 10, 2015 at 21:36
  • thx for that...i know, i should learn a bit, but as well do i learn from all your worthfull comments and addings.... Commented Dec 10, 2015 at 22:11
  • 2
    Like others have said, you need to add asserts in. What is the exact problem you are having though?
    – dphil
    Commented Dec 14, 2015 at 15:11

2 Answers 2

1

The class you posted after your edit looks OK, but it's a bit overengineered. I don't know exactly what are the required fields on your Invoice object, but you could get away with a much simple test method.

  • There are objects that are not directly referenced or required by your trigger. Perhaps you don't need to instantiate them. I'm talking about the PricebookEntry, the OpportunityLineItem and the ONB2__InvoiceCounter__c.

  • You're adding some nonrequired fields in Opportunity (unless you've made them required), which again are not used by the trigger. You can get away without the optional fields like Billing information, etc.

On top of that, you should test other cases, like:

  • Bulk inserts/updates: what happens when you create/update more than 150 records? Is your trigger bulkified or will it go over the governor limits. A good way to test this is to create a test that inserts/updates 151 opportunities in a single DML operation.

  • Null/invalid case: what happens if you insert a value that's acceptable in a.OppID__c but not in a.Opportunity__c?

These are all interesting cases to cover.

I wrote a little blog post about this, which perhaps you might find useful.

-3

Because it's a trigger test, you simply need to write a class and insert the object:

@isTest
public class TEST_ONB2__Invoice__c  {
static testMethod void TEST_ONB2__Invoice__c_trigger()
{
    insert new ONB2__Invoice__c(name='testname',OppID__c='someID');
}

Of course, this assumes that only the name field is required, and it's not an autonumber

6
  • 1
    Hey again HungryBeagle - I downvoted this post. While the code you provided will provide coverage (although it won't hit the actual assignment line), it encourages so many poor practices that lead to future headaches for developers like us down the road :) If you amend your post to reflect best practices, I'll be happy to amend my vote Commented Dec 10, 2015 at 21:29
  • I updated it to cover the additional line. Commented Dec 10, 2015 at 21:34
  • 2
    I removed my downvote (there was one other than mine), but a good test would also have asserts, as well as test a bulk situation as well. Commented Dec 10, 2015 at 21:35
  • 4
    A test without asserts is not a test.
    – Adrian Larson
    Commented Dec 11, 2015 at 1:34
  • This is not a good test method as it is not asserting the behaviour of the trigger. As @Brian Mansfield said, it just encourages bad practices
    – mkorman
    Commented Mar 23, 2016 at 22:19

You must log in to answer this question.

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