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
Markey1Markey1 

Apex Class Test Coverage - Need Help with Snippet

How do you test messages? I'm getting no coverage for the lines highlighted in red. Any help with how to write a test condition for the below code is much appreciated.

 

public PageReference save() {
  try {
    if(skTest.Submission_Type__c == 'New Submission' && (attachment.name == '' || attachment.description == '' || attachment.body == null)) {
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'All upload fields are required.'));
    return null;
  }

 

Cory CowgillCory Cowgill

The IF statement conditions are not being met.

 

You need to set the skTest.Submission_Type__c = 'New Submission' and you need to set the attachment.body = null in your test method.

 

Setup your test data properly to evaluate to true for that IF condition and your code will be tested.

Markey1Markey1

Thanks for your reply Cory. This code was not written by me and I'm still fairly new to Apex/programming but I will give it a shot and report back how it works out.