I'm working on the APEX TRIGGER TESTING challenge , and I validate the challenge with a very bad code (to be honest, I saw that I got 100% coverage and I was really surprised, try to validate and the challenge pass),

I'd like to understand why it passed because there is many things I don't understand ;

Here is my code :

private class TestRestrictContactByName {

    Static void SetUp() {
        // en fait non utilisé mais bon pour entrainement

    Static testmethod void TestAjoutListe () {

        // le premier contact doit passer, le second non
        List <Contact> contList2 = new List <Contact> {
            new Contact(firstName='Al', lastName='Douz'),
            new Contact(firstName='DonneeTest', lastName='INVALIDNAME')};

        // perform test : tentative d'upsert
        Database.UpsertResult[] UpResult = database.upsert(contList2, false);

        // on boucle sur les resultats
        // On doit pouvoir boucler sur les bons resultats ?
        // Comme on est en test, pas sur, car la query ne fonctionne pas
        for (Database.UpsertResult dr : UpResult) {
            if(dr.isSuccess()!=true) {
                System.debug('Si tu passes ici c\'est bien que l\'opération n\'est pas un succes');           

            // verif de la taille de la liste
            // System.debug('Taille liste err ' + UpResult. getErrors().size());
            //System.assert(UpResult.getErrors().size() = 0);

            System.debug('Taille liste');



So my questions are : - how can people only test with one contact, that is INVALID ; don't we have to test if the trigger doesn't interface with a good insertion ?

  • also, all the code I can find on the internet (e.g. here or here), only test the INSERT (what about the before update trigger ??) ?!

for the assertEquals stuff, I've to read the documentation before asking ; I think I still have an issue understanding how works the Database.SaveResult (or Database.UpsertResult) objects ... Is there a list of lists, in which there are couples (objectToUpsert, SuccessOrNot) ?

Thanks again !


You've asked a good question that doesn't necessarily relate to passing the specific challenge, so I'll try to answer it for you by focusing on the practices you'll want to utilize when creating unit tests in the future.

First, your unit test should be using separate methods for the specific conditions you want to test, including both positive and negative test conditions. By using database.upsert() it appears that you were attempting to test both BeforeInsert and BeforeUpdate conditions for the trigger. Unfortunately, that isn't the way an Upsert() works because by not having an Id, your records were only being inserted.

Many prefer to have methods to test when a condition like if(c.LastName == 'INVALIDNAME') is True and another for when it's False. Instead, you utilized data that tested both in the same method. There's nothing "wrong" with that approach, but what you didn't do is get the results and assert them (your code for that is commented out).

If you'd have done it in a separate method, handling the results may have been easier for you to do. In using a list, what you needed to know is that contList2[0] would have returned an Id (Success) when successfully inserting a record in the Database.UpsertResult and that contList2[1] would have returned the error: 'The Last Name INVALIDNAME is not allowed for DML'. Those are both things you'd have been able to assert in the unit test.

In a BeforeUpdate test method you'd have had an Id to work with that would have made that very easy to iterate on. With a BeforeInsert unit test method, you need to iterate on errors a bit differently since you don't have an Id for a record that doesn't insert. In that situation, one has to use the original list to figure out which record now has an Id and which one(s) generated the error messages. (you can use a map or iterate with a counter).

I hope this explanation helps you understand what you could have done differently in your unit test, both to improve it and achieve the kind of results you'd had preferred. Also, your update method would have taken a valid contact that you'd first inserted. In the update method you'd have changed the last name to INVALIDNAME to cause the error message.

One last observation is that you might want to take a look at the required fields for Contact. Your unit test will need to satisfy those requirements too.

| improve this answer | |

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.