4

While preparing myself to the Developer 1 certification I met the following question:

A developer is asked to write negative tests as part of the unit testing for a method that calculates a person's age based on birth date. What should the negative tests include?

A) Throwing a customer exception in the unit test

B) Assert that a null value is accepted by the method

C) Asert that past dates are accepted by the method

D) Assert that future dates are rejected by the method

I gave answers A) and D), while the correct answer is only D).

So, I checked the negative tests definition here:

Negative testing ensures that your application can gracefully handle invalid input or unexpected user behavior. For example, if a user tries to type a letter in a numeric field, the correct behavior in this case would be to display the “Incorrect data type, please enter a number” message. The purpose of negative testing is to detect such situations and prevent applications from crashing. Also, negative testing helps you improve the quality of your application and find its weak points.

The core difference between positive testing and negative testing is that throwing an exception is not an unexpected event in the latter. When you perform negative testing, exceptions are expected – they indicate that the application handles improper user behavior correctly.

And also I checked the positive tests definition in the same article:

Positive testing determines that your application works as expected. If an error is encountered during positive testing, the test fails.

So, I infer from the definition of positive testing that B) and C) are wrong answers - they do not test "negative" input, they test the input that gets accepted and processed as expected.

D) is clearly the negative testing because it is about not accepting the unexpected input. And I consider A) to be a part of a correct answer, since the exception may be thrown only after getting an unexpected input, which makes me think that it is a negative testing as well.

Help me, please, to figure out what I am missing here.

7

Usually, we don't throw exception from the testmethods, rather we catch the exception and put them in proper way through assert statement.

like this:

public class MyClass
{
    public String accountId {get;set;}

    public void myMethod()
    {
        if(accountId == null)
        {
            throw new exception ('Account Id is blank');
        }
    }

}

test class to handle exception

public static testmethod void  mytest()
{
    MyClass cls = new MyClass();

    cls.accountId = null;

    try
    {
        cls.myMethod();

    }
    catch (Exception ex)
    {
        if(ex.getMessage().contains('Account Id is blank')
        {
            System.assert(true);    
        }
        else
        {
            System.assert(false);
        }

    }

}

So, it boils down to option D, eliminating all other options as follows:

A) Throwing a customer exception in the unit test - This cannot be good option

B) Assert that a null value is accepted by the method - This is not a testing negative scenario

C) Asert that past dates are accepted by the method - This is positive test

D) Assert that future dates are rejected by the method - This is correct way for doing negative test.

  • 2
    Technically, this unit test will fail if and only if there are at least an exception thrown. It should fail also if there are no exception – kurunve Aug 3 '18 at 8:10

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.