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
javierjiesjavierjies 

My very first Test Method on a Trigger

trigger HonorariosAutomatico on Factura__c (before insert, before update)
{
  for (Factura__c fac: Trigger.new)
  {
    for (Tarifa__c tar: [select id, Corresponde__c from Tarifa__c where id = :fac.Tarifa__c])

    {
        fac.Honorarios__c = tar.Corresponde__c;

    }
  }
}



private class TestNewFactura
{
    static TestMethod void MiTest()
    {
           Factura__c f=new Factura__c();
           for (Tarifa__c t: [select id, Corresponde__c from Tarifa__c where Name = 'T-001'])
           {
               f.Honorarios__c = t.Corresponde__c;
               insert f;   
           }
           Integer prueba=[select Id from Factura__c where Honorarios__c =: f.Honorarios__c];
           if (prueba)
           {
               System.assert(true);
           }
           else
           {
               System.assert(false);
           }
    }
}

 

 

 

 

 

Error: Compile Error: expecting EOF, found 'private' at line 14 column 1

 

 

What's the matter?!?!?

 

Thank you ;)

Michael_KahleMichael_Kahle

Hi javierjies,

 

you can not put more than one class or trigger into one File. You need to create an extra Class-File for the TestClass .

javierjiesjavierjies

ok!

 

So now I have the Trigger HonorariosAutomatico

 

trigger HonorariosAutomatico on Factura__c (before insert, before update) 
{
for (Factura__c fac: Trigger.new)
{
for (Tarifa__c tar: [select id, Corresponde__c from Tarifa__c where id = :fac.Tarifa__c]) {fac.Honorarios__c = tar.Corresponde__c;}
}
}

 

and the Appex Class  TestNewFactura.cls

 

public class TestNewFactura 
{
static TestMethod void MiTest()
{
Test.startTest();
Factura__c f=new Factura__c();
for (Tarifa__c t: [select id, Corresponde__c from Tarifa__c where Name = 'T-001'])
{
f.Honorarios__c = t.Corresponde__c;
insert f;
}
Factura__c prueba=[select Id from Factura__c where Honorarios__c =: f.Honorarios__c];
if (prueba != NULL)
{
System.assert(true);
}
else
{
System.assert(false);
}
Test.stopTest();
}
}

 

 

But It keep saying:

 

Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required

 

 

Any Idea?!?!?

 

 

Thanks!!!!!!!!!

 

 

Michael_KahleMichael_Kahle

To get the coverage you would need to deploy both, the test class AND the trigger. The test is then run in the Class and would trigger.

It also may be, that there is no DataSet with the Name 'T-001'.

 

Why not just Create a new Factura with an hardcoded value like and do not forget to set the Tarifa Id, as this is what you are checking at in the trigger :

 

 

public class TestNewFactura {
static TestMethod void MiTest()
{
Tarifa__c testTarifa = new Tarifa();
testTarifa. Corresponde__c = 'AnyValue';
insert testTarifa;
Factura__c f=new Factura__c();
f.Honorarios__c = 'SomeValue';
f.Tarifa__c = testTarifa.Id;
insert f;
}
}

 

 

 

 

 

 

This should fire the trigger and copy the value.

 

In the test Method you should always create ANY Object you need new and not use existing ones. It is more to Code but saver to deploy.

 

Also the Trigger is a little bit off. You should code it bulk secure, you sure get an error with this Trigger, if you insert a bulk of more than 20 or 30 Factury DataSets at once. Iterate through the Trigger and get all Tarifa__c Ids first and create a Map for them. This way it is bulk secure.

 

 

I hope this helped.

 

 

 

javierjiesjavierjies

First of all, thank you for your help!!

 

If I do this query: Select t.Corresponde__c From Tarifa__c t

I see all Corresponde__c values: 225.0, 660.0, 150.0 .......

 

 

 

I have change TestNewFactura.cls to this:

 

public class TestNewFactura { 
static TestMethod void MiTest()
{
Tarifa__c testTarifa = new Tarifa__c();
testTarifa.Corresponde__c = 150.0;
insert testTarifa;
Factura__c f=new Factura__c();
f.Honorarios__c = 1234;
f.Tarifa__c = testTarifa.Id;
insert f;
}
}

 

 

But It keep giving an error message:

 

 

 Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required

 

 

 Help!! I'm desperate!!

 

Thanks ;)

Michael_KahleMichael_Kahle

You need to run the Tests on the TestClass. You also need to deploy the Test Class along with the Trigger into Production Instances.

If you then write another Trigger, you can just extend the Test Method and deploy it again to get coverage for new Triggers.

javierjiesjavierjies

Hi!!

 

I've develop a complete Apex Class to check all the triggers. When I click on"Run Test", It says:

 

Average test coverage across all Apex Classes and Triggers is 63%, at least 75% test coverage is required

 

But, when I try to do the Package, It says about triggers:

 

Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required

 

And, at the end: 

 

Average test coverage across all Apex Classes and Triggers is 0%, at least 75% test coverage is required

 

 

 

What am I doing wrong?!?!?

 

Thanks in advance!!