發表文章

目前顯示的是有「null」標籤的文章

[踩坑] Test Class 失敗原因之一:抓不到 Custom Setting 的 data

 症狀描述 Apex Class明明寫好了,實測功能也都正常。但是要 deploy 到 Production 的時候,對應的 Test Class 一直 fail 。 仔細看了出錯的行數,問題是出在 Custom Setting 抓到的值竟然是空值。 EXCEPTION_THROWN [103]|System.NullPointerException: Argument 1 cannot be null 檢查過了 Production 的 Custom Setting,欄位已經預先開好了,值也填進去了。 在 Production 的 debug console 以 Execute Anonymous Window 測試,確實也可以用 system.debug 成功地把 Custom Setting 的值打印出來。 關鍵 Custom Setting 看似是個系統 setting ,但他本質仍然就是只是個 Custom Object。如果 Test Class 沒有宣告為 @isTest(SeeAllData=true) 的話,一樣是抓不到任何的 data 的。 所以主要解法有二: 把 Test Class 加上@isTest(SeeAllData=true) 在 Class 裡針對 Test Class 寫個 IF 去處理它 參考資料 How to get Custom Settings in Apex Test code? https://salesforce.stackexchange.com/questions/9565/how-to-get-custom-settings-in-apex-test-code