0

I have a test class that works on my org dev, I'm trying to make deploy in another org but it's having a strange behavior, the error is in the first assertEqual:

@isTest
    static void testUserStore() {
        List<String> stores = new List<String>();
        stores = OrderController.getStores();

        System.assertEquals(stores.contains('Store 1'), true);
        System.assertEquals(stores.contains('Store 2'), true);
    }

System.AssertException: Assertion Failed: Expected: false, Actual: true Stack Trace: Class.OrderControllerTest.testUserStore: line 81, column 1

The error doesn't make sense because I'm expecting true, not false, and the result were true!! What's wrong?

I tried to change to only assert, but the error is the same:

@isTest
        static void testUserStore() {
            List<String> stores = new List<String>();
            stores = OrderController.getStores();
    
            System.assert(stores.contains('Store 1'), true);
            System.assert(stores.contains('Store 2'), true);
        }

System.AssertException: Assertion Failed: true Stack Trace: Class.OrderControllerTest.testUserStore: line 81, column 1

3
  • 3
    The issue with the message in the assertion's failure message is because you've reversed the arguments. It's assertEquals(Any expectedValue, Any actualValue), but you're putting the "actual value" (stores.contains(...)) as the first argument. Since you're just doing a true/false here, assert() is the appropriate method to use. The third argument (or second argument in case of the plain assert()) is optional but allows you to provide a message to display if the assertion fails.
    – Derek F
    Commented Apr 10, 2023 at 15:16
  • Educated guess: getStores is retrieving some kind of metadata, probably Custom Metadata, that was not included in the deployment. Without seeing the getStores method, we wouldn't be able to say anything more definitive. The assertions are not the primary problem, but rather a symptom of the real problem.
    – sfdcfox
    Commented Apr 10, 2023 at 15:19
  • As for why the assertion is failing in your target org, we don't have enough information to say why that is for certain. You would need to share OrderController with us. Since I don't see seeAllData=true (which should almost never be used), I'll guess that you're trying to pull data from a Custom Metadata Type. If that's the case, then you need to deploy your MDT object (part of the CustomObject fol0der) as well as the MDT records (part of the CustomMetadata folder).
    – Derek F
    Commented Apr 10, 2023 at 15:20

1 Answer 1

1

The reason you find the assertion confusing is that you have inverted the order of the first two arguments. The proper argument order is:

system.assertEquals(expected_value, actual_value, informative_message);

So you don't even have a message, as you are using only two arguments, and you need to invert the order of those arguments.

Normally, for a unit test, you should do some data setup, which is not shown in your question. And based on that data setup, you usually know exactly what values you expect. So it might be useful to have List<String> expected_output and then do assertEquals(expected_output, stores, 'added message here').

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .