1 min read

Unit Testing: Don't forget about custom settings

I ran in to a nightmare of an issue with custom settings. I had created my custom settings objects and used them in my code. Everything worked great. Unit tests passed and I thought I was ready to deploy. When I tried to deploy I started getting a lot of strange errors on my unit tests. Mainly on my negative unit test methods that were meant to catch a specific error but instead got an unexpected error. I combed over all my code and started taking out assertions trying to find what the issue was.

After pulling my hair out and cursing to myself I realized that I was attempting to use the custom settings that existed in my sandbox environment. Because you can create and update custom settings like you would any other object I went ahead and created entries or updated existing entries to contain the data that I needed during a unit test method call. It solve my issue and I was able to validate my deployment.

Just a little tip for those working with custom settings and a reminder to myself to make sure custom settings are part of my unit test setup methods. :)