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
David OvellaDavid Ovella 

How to test this trigger? is giving me unexpected token: @

This is a "trigger" that doesn't allow duplicate values of the field Pre_Impreso 003_001 _c
trigger MensajeValorDuplicado on Factura__c (before insert, before update) {
    
    Map<Decimal, Factura__c> facMap = new Map<Decimal, Factura__c>();

    For (Factura__c facturarecoleta : System.Trigger.new) {     
        If ((facturarecoleta.Pre_impreso_003_001__c != null) && (System.Trigger.isInsert)){            
            facMap.put(facturarecoleta.Pre_impreso_003_001__c, facturarecoleta);                
        }                  
    }
  
    For (Factura__c facturarecoleta : [SELECT Id, Name, Sucursal__c, Pre_impreso_003_001__c FROM Factura__c WHERE Pre_impreso_003_001__c IN :facMap.KeySet()]){
        If (facturarecoleta.Sucursal__c=='Maker Recoleta'){
            Factura__c newFactura = facMap.get(facturarecoleta.Pre_impreso_003_001__c);
            newFactura.addError('El #Pre-Impreso(003-001) ingresado ya fue utilizado en la Factura', false);
        }
    } 
}

I tried to test this trigger but isn't allowing me to save because of the @isTest 
error: unexpected token: @
Also I changed the version of the trigger but still doesn't work

my trigger test (I don't know why isn't recognizing the @isTest)
@isTest
private class MensajeValorDuplicadoTest {
    static testMethod void TestMensajeValorDuplicado() {
   
       Test.startTest();
      
       Map<Decimal, Factura__c> facMap = new Map<Decimal, Factura__c>();
       Factura__c facturas = new Factura__c();
       //insert factura;

       For (Factura__c factura: [Select ID, Name, Pre_Impreso_003_001__c, Sucursal__c, Caja__c From Factura__c WHERE Pre_impreso_003_001__c IN :facMap.KeySet()]){
           System.debug('factura name' + factura.Name);
           System.assertEquals('factura name',factura.name);
           System.debug(555555 + factura.Pre_Impreso_003_001__c);
           System.assertEquals(55555,factura.Pre_Impreso_003_001__c);    
           System.debug('Caja 1' + factura.Caja__c);
           System.assertEquals('Caja 1',factura.Caja__c);   
           System.debug('Maker Centro' + factura.Sucursal__c);
           System.assertEquals('Maker Centro',factura.Sucursal__c);            
      }
    
       Test.stopTest();
    }
}

 
pconpcon
Your test class has to be in it's own class (in this case called MensajeValorDuplicadoTest) it is not included in the trigger code.
David OvellaDavid Ovella
thanks for answer,
so my test has to be inside my trigger?
or inside what class?

 
trigger MensajeValorDuplicado on Factura__c (before insert, before update) {
    
    Map<Decimal, Factura__c> facMap = new Map<Decimal, Factura__c>();

    For (Factura__c facturarecoleta : System.Trigger.new) {     
        If ((facturarecoleta.Pre_impreso_003_001__c != null) && (System.Trigger.isInsert)){            
            facMap.put(facturarecoleta.Pre_impreso_003_001__c, facturarecoleta);                
        }                  
    }
  
    For (Factura__c facturarecoleta : [SELECT Id, Name, Sucursal__c, Pre_impreso_003_001__c FROM Factura__c WHERE Pre_impreso_003_001__c IN :facMap.KeySet()]){
        If (facturarecoleta.Sucursal__c=='Maker Recoleta'){
            Factura__c newFactura = facMap.get(facturarecoleta.Pre_impreso_003_001__c);
            newFactura.addError('El #Pre-Impreso(003-001) ingresado ya fue utilizado en la Factura:<a href=\'https://na18.salesforce.com/'+facturarecoleta.Id+'\'>'+facturarecoleta.Name+'</a>', false);
        }
    } 


    static testMethod void TestMensajeValorDuplicado() {
   
       Test.startTest();
      
       Map<Decimal, Factura__c> facMap = new Map<Decimal, Factura__c>();
       Factura__c facturas = new Factura__c();
       //insert factura;

       For (Factura__c factura: [Select ID, Name, Pre_Impreso_003_001__c, Sucursal__c, Caja__c From Factura__c WHERE Pre_impreso_003_001__c IN :facMap.KeySet()]){
           System.debug('factura name' + factura.Name);
           System.assertEquals('factura name',factura.name);
           System.debug(555555 + factura.Pre_Impreso_003_001__c);
           System.assertEquals(55555,factura.Pre_Impreso_003_001__c);    
           System.debug('Caja 1' + factura.Caja__c);
           System.assertEquals('Caja 1',factura.Caja__c);   
           System.debug('Maker Centro' + factura.Sucursal__c);
           System.assertEquals('Maker Centro',factura.Sucursal__c);            
      }
    
       Test.stopTest();
    }
}

if it is like this, is giving me an error: Only static top-level class methods can be test methods
 
pconpcon
No, you have to have a separate class.  So you would create your trigger (File -> New -> Apex Trigger in the developer console).  Then you would create a new class called MensajeValorDuplicadoTest (File -> New -> Apex Class in the developer console).
David OvellaDavid Ovella
I did that before, but when I tried to deploy to production my trigger had a problem of the coverage code.
74% normal without a test, then I created the test with apex class, I put both to deploy but still I got 74% of coverage code. So maybe, I am connecting wrong the trigger with his apex test class.
pconpcon
Well your test doesn't actually run your code.  If you look on line 9 of your test (from your original post) you can see that the insert is commented out.  Therefore your trigger will not fire for this test code.  Additionally it will not cover a good chunk of your trigger because your test does not generate any Factura__c that meet the criteria of your query on line 11 therefore your loop will never get entered.