2

I have approximately 20 lines of code which inform the user if one of the 3 files they upload are too big.

public PageReference submitCase() 
{
    //if any files are too big respond with message
    if(ApexPages.hasMessages(ApexPages.severity.ERROR)) { 
        string file_names='';                
        string other ='';                
        integer i=0;
        for(Attachment a: allFileList)
        {
            if(a.Name != null)
            {
                if(i>0)
                    file_names += ' and '; 
                i=i+1;
                file_names += a.Name;         
            }
        }
        if(i>0)
        {
            file_names = 'reload ' + file_names;
            if(i>1)
                file_names += ', ';
            file_names += ' and ';
            other ='other ';                
        }    
        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'One or more of your files returned the message above. Please ' + file_names + 'choose a smaller version of the ' + other + 'file(s)');
        ApexPages.addMessage(msg);           
        return null;
    }

    //rest of function
}     

I need a way to test this part in order to get proper code testing coverage.

I have seen how to simulate a file upload but not an over-sized file upload. Any ideas?

Thank you.

5
  • what would I do in your place -- do not simulate file size error. Just add page message with error to page and execute this method. There is no difference at this point I think
    – kurunve
    Dec 22, 2015 at 15:19
  • @kurunve I don't understand what "Just add page message with error to page and execute this method" means please clarify.
    – Aba
    Dec 22, 2015 at 15:38
  • The code snippet that you've provided is (I assume) the method that is called after you've detected that a file is too large. There's not much to test in that method beyond testing that the page message contains the expected file names. To help you out, we'll need to see the code that actually detects the size of the files. I suspect this will be a case for refactoring and using dependency injection.
    – Derek F
    Dec 22, 2015 at 15:39
  • @DerekF if(ApexPages.hasMessages(ApexPages.severity.ERROR)) is the code which detects the over-sized file any other suggestion I came a across i.e. (I think - a.BodyLength== 0 a.name != '') didn't work as both were null. I only found this by accident.
    – Aba
    Dec 22, 2015 at 15:48
  • @Aba I see, I missed that part. In that case, kurunve's suggestion is what you'll end up doing. I'll write up an answer to clarify.
    – Derek F
    Dec 22, 2015 at 15:56

1 Answer 1

1

Relying on a pageMessage to detect your error state might not be the best idea, but without seeing more of your code, I can't make a better suggestion (at least not with reasonable certainty that it would work for you).

Since your code relies on receiving a pageMessage of severity ERROR, you'll need to inject a pageMessage of that severity in your test method. To do this, you'll additionally need to use Test.setCurrentPage()

The general structure of test method will look like this:

static testMethod void testTooLargeError(){
    // Perform any test setup that you need, creating and inserting
    // `Attachment` records will be a part of this step

    // Create a pageReference instance for the visualforce page that uses the controller
    // (or controller extension) that you are testing
    PageReference pageRef = Page.myVisualforcePage;
    // Set any page parameters that you need
    pageRef.getParameters().put('someParam', 'someValue');

    // This line is what will allow your controller (or controller extension) to see
    // and use pageMessages and parameters
    Test.SetCurrentPage(pageRef);

    // If you're testing a controller extension, you need to create a standardController instance first, 
    // then pass the standardController into your controller extension
    ApexPages.StandardController sc = new ApexPages.standardController();
    myControllerExtension controllerExtension = new myControllerExtension(sc);

    // The final bit of setup, add the pageMessage
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'some error message'));

    // Now, call the method you want to test
    Test.startTest();
    controllerExtension.submitCase();
    Test.stopTest();

    // Time to verify that your method behaved as expected.
    // Iterate through all the pageMessages, and ensure that the message you expect resides amongst them.
    Boolean hasExpectedMessage = false;
    for(ApexPages.Message message: ApexPages.getMessages()){
        if(message.getSeverity() == ApexPages.Severity.INFO && message.getDetail() == 'some error message'){
            hasExpectedMessage = true;
        }
    }

    System.assertEquals(true, hasExpectedMessage, 'Expected pageMessage for oversized file was not found');
}
3
  • So you are saying there is no way to simulate an over-sized file but since I'm not really testing for it but just the existence if an error I can push the error to the controller and then test the results. (By the way, trust me there really isn't any other relevant code, the rest is just processing the users input and creating a case out of it.)
    – Aba
    Dec 22, 2015 at 17:04
  • I think you might be getting stuck on trying to test the platform rather than your code. From the sound of it, the platform handles rejecting the over-sized upload, and then your code comes along, detects that rejection, and adds additional information. If that's correct, then attempting to do something like insert new List<Attachment>{new Attachment(BodyLength = 10000000000)}; in your test is testing the platform as much as it is testing your code. Injecting the pageMessage in this case is the same thing as simulating an over-sized file.
    – Derek F
    Dec 22, 2015 at 17:52
  • For if( ... && message.getDetail() == 'some error message') I used if(message.getSeverity() == ApexPages.Severity.INFO && message.getDetail().indexOf('One or more of your files returned the message above') > -1) - Thanks
    – Aba
    Dec 22, 2015 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.