0

I'm very noob and I'm writing a simple trigger (and I need to know if it works before re-writing in bulk mode). So, in order to have the trigger deployed, I wrote a simple (not useful) test class, but I still get this error:

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

TRIGGER:

trigger UpdateQuotationRollups on Q_Product__c (after insert, after undelete, after update){
    Quotation__c theParent = [SELECT Name, Agent_hours__c from Quotation__c 
        where Id = :Trigger.newMap.keySet()];

    AggregateResult a = [SELECT SUM(Agent_hour__c)somma 
        From Q_Product__c WHERE Quotation__c=:theParent.id];

    Decimal ore = 0;

    String str = '' + a.get('somma');
    ore = Decimal.ValueOf(str);

    theParent.Agent_hours__c = ore;
}

CLASS:

@isTest 
public class testUpdateQuotationRollupsTrigger {

    static testMethod void insertNewChild() {
        Q_Product__c nuovochild = 
            new Q_Product__c(Name='fortestChilds',Quotation__c = 'a0WD000000EbSpu');

        insert nuovochild;
    }

}
4
  • Have you added the test class into the change set?
    – highfive
    Jun 20, 2014 at 9:33
  • I'm working by eclipse directly in production environment
    – user9959
    Jun 20, 2014 at 9:34
  • Not sounds good. If you are deploying the trigger for the first time you should add the test class as well so that while deploying the test cases are run. Or you can just run the test class to check the code coverage before deploying
    – highfive
    Jun 20, 2014 at 9:37
  • I apologize for my low knowledge about this. Now, if I go in Apex Classes on the platoform and run the test for this class, it says : test Method passed
    – user9959
    Jun 20, 2014 at 9:40

3 Answers 3

0

You can't use record ids in your test class which look like what you are doing for quotation__c. The Record ids change every time. You'd have to do a query to get the id after you insert it if you need the id.

Note: Salesforce doesn't actually insert a record as you may know, but still generates an id for testing.

1

Before doing a deployment always run your test class so that you are confident with your code. You can go to your test class by Setup > Apex Classes > Your Class and click the Run Test button at the top.

When you are deploying the trigger( or class) make sure to include the relevant test classes for them since salesforce is validating your components before the deployment. In this stage you should have your test classes.

Since you said that this is a starting of writing the trigger it's ok go ahead and improve your trigger by bulkyfing and etc. Hope this link also help you.

0

Create a quotation record in your test first. After you insert you will be able to reference the resulting id. Then use that instead of hard coding an I'd like users2451 mentioned above. If you use the developer console to run your test you can see the coverage on the right hand side. But I'm not sure you want to deploy the trigger as it is, add a system assert to your test to make sure it's doing what you think it should. Have you tested this in the sandbox user interface to make sure it does what you expect it to?

You must log in to answer this question.