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
MrTheTylerMrTheTyler 

Designing unit tests that won't break on addition of required fields

I was curious to know if there is a best practice for designing unit tests that can handle the addition of new required fields to an object. For example, let's say that I have an insert trigger defined on a opportunity object.  so, in order to get complete code coverage I develop a test method which creates a dummy opportunity record. At the time of writing this test method I make sure that the dummy record has all the necessary fields included.  the dummy record is inserted and causes the insert trigger to fire and everything works fine. Let's say six months later a new field is added to the opportunity object and it is a required field. Would that not cause my test method to be invalid?

 

Just curious if anyone out there has experienced this issue in the past and what technique you used to deal with this circumstance.

 

~Tyler

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

We have experienced this in the past, especially as we implement systems and then hand them over to clients and act as 3rd line support, so they can add in fields of this nature at will.

 

We usually take the view that its not worth investing the time and effort to second guess all this kind of stuff, as things like validation rules can easily break unit tests too and I'm not aware of any way to detect that.  Instead, we run the unit tests on the production system on a scheduled nightly basis and take action when a test breaks.

All Answers

bob_buzzardbob_buzzard

We have experienced this in the past, especially as we implement systems and then hand them over to clients and act as 3rd line support, so they can add in fields of this nature at will.

 

We usually take the view that its not worth investing the time and effort to second guess all this kind of stuff, as things like validation rules can easily break unit tests too and I'm not aware of any way to detect that.  Instead, we run the unit tests on the production system on a scheduled nightly basis and take action when a test breaks.

This was selected as the best answer
MrTheTylerMrTheTyler

Thank you for the insight!

Tim__mTim__m

Hey Bob,

How do you deal with managed packages with unit tests that fail because of validation rules, required fields, and such? I have been doing nightly unit test runs on production as you do but always have to sift out failed managed package unit test.

bob_buzzardbob_buzzard

That's a good question - we normally try to get the developer's of the managed package to fix the tests, as I believe you can still hit issues when deploying code because of managed package unit test failure.