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);
}
}