-1

I am developing a Test classes for the Milestones and Entitlement processes by taking a reference from : Can we merge if else condition in a single test class method? . I am getting error

Expression cannot be a statement  

The test class below for reference::

public static testMethod void testQQQQ(){
        Test.startTest();
        Account acc = new Account(Name='Account');
        insert acc;

        .............
        .............

        Case c = new Case();
        ..........
        ..........

        List<Case> cases = new List<Case>();
        cases.add(c.clone(false));
        ................
        ..............
        insert cases;

        cases[0].Status = 'XXXX';
        cases[0].SubStatus = 'XXXX';
        update cases;
        cases[0].SubStatus != 'YYYYY'; // line-22
        update cases;                
        Test.stopTest();
    }
}

Basically, I wanted to test the below condition::

if(oldCs.Status == 'XXXX' && oldCs.SubStatus == 'XXXX' && cs.SubStatus != 'YYYYY'){
    cs.addError(OOOOOOOOOOO');
}

3 Answers 3

4

The only valid operations for assigning a value to a field are assignment operators, which are "=", ">>=", "<<=", ">>>=", "&=", "|=", "+=", "-=", "*=" and "/=". Other operators with equals, such as "!=", "==", "===", "<=", ">=", are Boolean operators, and cannot assign a value to a field. Simply assign any value that's not 'YYYYY' (case insensitive) in order to pass the test.

For example, the following would pass:

    cases[0].SubStatus = 'YYYYZ'; // line-22
4
  • Actually I need to strictly checked the condition for cs.SubStatus != 'Request Submitted' , because otherwise my validation is not passing. Aug 15, 2017 at 13:07
  • @user4567570 If there's validation preventing it, then it's not possible for your code to execute. You can't test impossible conditions.
    – sfdcfox
    Aug 15, 2017 at 13:56
  • Any workaround in setting value cs.SubStatus != 'Request Submitted' in test class ? Aug 15, 2017 at 13:58
  • @user4567570 You'd have to set it to a specific value, like cs.SubStatus = 'Request Not Submitted'. If you're not able to set such a value because of validation, you won't be able to test your code.
    – sfdcfox
    Aug 15, 2017 at 13:59
0

Apex has this limitation. you can't use conditional expression without assigning to a variable or using it in a condition statement (while, if, etc). For example, none of the following should compile when written as stand-alone expressions.

i != 95; i < 75; i > 75;

And as per your code what line 22 defines ? it has to be in if or some another conditional block as per apex.

5
  • Very weird limitation. It stop developer to test complex things for sure ! Now Guide me how I will test that condition ? Aug 15, 2017 at 11:09
  • what is that condition you want to test ? You are inserting cases. Then changing First case record status to 'xxxx'. Aug 15, 2017 at 11:12
  • Added details above in my posts. remember I am working on Entitlements, so I need to check all the status & sub status values while saving the case. Aug 15, 2017 at 11:13
  • missing this ' in addError('0000000') Aug 15, 2017 at 11:15
  • You've not posted the code you're testing. From what you have shared, best thing would be to query the results after Insert for the condition and assert that it's != to 'yyyy'.
    – crmprogdev
    Aug 15, 2017 at 11:16
-1

I'm not sure what you're trying to test since your question is not clear, but as @sfdcfox said, you're using a boolean operator as if it were an assignment operator.

Assuming you want to do an assignment and something similar to the C# ?? operator, you have to do it the long way:

x = (y != null) ? y : z;

In case you're trying to test your code with assertions, then you might be looking for the System assertion methods:

System.assertNotEquals('YYYYZ', cases[0].SubStatus);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .