Find your content:

Search form

You are here

Testing unwriteable fields


I am trying to write tests for code that depends on the Status picklist field of EmailMessage objects. The Status field is unwriteable.

For testing purposes, I have been able to create an 'EmailMessage' object with a specific Status value, but am unable to alter it after that. I really need to test how other objects react to a change in Status of these EmailMessage objects and wonder what are the patterns for these situations where the actual fields are unwriteable after construction?

As an aside, I thought I'd mention that I was surprised that the values for these fields were numbers that must map to the actual values shown for the picklist ('New', 'Read' etc).

Many thanks

Attribution to: Joe

Possible Suggestion/Solution #1

The wrapper pattern could provide a handy solution

Public with sharing class EmailMessageWrapper{

EmailMessage emailMsg;

Public EmailMessageWrapper(){
this.emailMsg = new EmailMessage();

// instance variable for immutable fields
Public String status { get ; set; }

Public String writeAbleField ( get { return        
emailMsg.WriteableField; } set { 
this.writeAbleField= value;} ) //check syntax


You can then use this wrapper for doing your tests, wrap results of soql queries before returning to processing methods.

Attribution to: techtrekker

Possible Suggestion/Solution #2

I am 99 % that this method Test.loadData would solve that problem.

Attribution to: PepeFloyd
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content