1

I m trying to cover and test my apex code so I developped a test class to test my insertion, my the test is failed unfortulately; and gave me this error: Assertion error

here is my test class:

@isTest
private class TestTimeOff {

    static testMethod void myTimeOffTest() {
        Test.startTest();
         Time_Off__c to = new Time_Off__c();
            to.Start_date__c = Date.newInstance(12, 05, 2015);
            to.End_date__c = Date.newInstance(13, 05, 2015);
            //to.My_back_up__c = 'XXXX';
            to.Pattern__c = 'Payed time off';
            to.Comment__c='My Class Test';
      try{
             insert to;
        }
        catch(System.DMLException e){

        System.assert(e.getMessage().contains('Insertion failed'));
       }
       test.stopTest();
 }

}

an other problem; My_back_up__c is a reference to a user, how can I do this? should I give the user ID??

p.s. I have trigger launched before and after adding a Time Off

Thank you

  • what assertion exception you are getting? And what these trigger do for time Off Object.You may inactive your trigger and then check weather the exception occurred because of Triggers . – userRg Mar 31 '14 at 9:39
  • 2
    Change your assert to "System.assert(e.getMessage().contains('Insertion failed'), 'message=' + e.getMessage());" then in the test failure information you will see the actual message. – Keith C Mar 31 '14 at 9:46
  • Same error even when I desactivated the triggers || Method Name myTimeOffTest || Error Message System.AssertException: Assertion Failed || Stack Trace Class.TestTimeOff.myTimeOffTest: line 17, column 1 – LoveLace Mar 31 '14 at 9:49
  • Start_date__c Invalid :O but how can I do it in another way, i'v tried this too to.Start_date__c='12/05/2014' no result as well – LoveLace Mar 31 '14 at 10:00
  • you can look at the answer for date type field.Hope it would help you! – userRg Mar 31 '14 at 10:12
1

You can use this for create a test user, in your test method.

    User u = new User(
                   FirstName = 'test',
                   LastName = 'test',
                   UserName = 'test@test.com.asdf',
                   Email = 'test@test.com',
                   ProfileId = UserInfo.getProfileId(),
                   Alias = 'TEST',
                   CommunityNickname = 'test.test',
                   EmailEncodingKey = 'ISO-8859-1',
                   LocaleSidKey = 'es_ES',
                   LanguageLocaleKey = 'en_US',
                   TimeZoneSidKey = 'Europe/Paris',
                   IsActive = true
                );
   insert u;

Can you tell us what is doing your trigger? i dont understand your assertion.

0

for My_back_up__c look up field you can directly query over the User Object without use of SeeAllData = true

You can follow the link:

https://stackoverflow.com/questions/11643324/how-can-i-create-an-user-with-system-administrator-profile-when-seealldata-fal

For Date Type Field in test cases, you can simply use

to_start_date__c = date.today();

to_ End_date__c= date.today().addDays(1);

  • Didnt understand this SeeAllData!! can you please explain it more – LoveLace Mar 31 '14 at 10:12
  • Test succesed!!! but didnt find this record in my time_Off__c object and the treatement with my triggers after insert is not working too!! – LoveLace Mar 31 '14 at 10:19
  • Records inserted within test cases are not visible at the UI. Actually the existence of these record are temporary and vanished after test execution completed. But t – userRg Mar 31 '14 at 10:37
  • But wot about triggers?? why are they not launched? – LoveLace Mar 31 '14 at 10:44
  • But when insert statement executed a database insert transaction committed which will automatically invoke triggers if present over Insert event (either before or after). If possible share the query you have used in your triggers which is showing null records. – userRg Mar 31 '14 at 10:45
0

This Post help me figure this out.

Triggering a DML Exception for Test Coverage You need to Modify your code slightly.

This finally worked for me. c is an invalid Contact that should be allowed in by trigger. The trigger throws a DML exception when it detects the invalid state.

    Test.startTest();
    try{
        if(Test.isRunningTest()){
            insert c;
        }
    }
    catch(System.DmlException e){    
    }
    Test.stopTest();
-4

If you want to use some already existing records for an object in test case then you need to use SeeAllData = True like as

@isTest(SeeAllData=true)

Using this you don't need to insert record in test cases ,what you need to do is simply query the records from the database like:

objectName ob = [select id, name ,... from objectName where predicates.. Limit 1];

But this should be avoided as if we deploy the code to any production then it will query for the record and if it not exist then test case will fail and ultimately your deployment will not succeeded.

  • 2
    No. We should not be advocating SeeAllData unless it's a must for off cases like Pricebooks. You should always create your own unit test data as needed and not rely on existing data. – James Loghry Mar 31 '14 at 13:54
  • Yes That's why I mentioned that it should be avoided.I have described it cz @Kawtar BRAZI was asking about SeeALLDATA.It was not belonging to an answer to the question. – userRg Apr 1 '14 at 5:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.