Throw Behavior

Top  Previous  Next

You can simulate exceptions using Isolator++ Professional quite easily using the WHEN_CALLED(...).Throw() combination. This is helpful if you want to check your error handling code. In the following example, we'll simulate an exception thrown when the GetAgeFromFile is called:

 

class Person

{

 

public:

   int GetAge() { 

      try

 {

         return GetAgeFromFile();

 }

 catch

 {

                  return -1;

 }

   int GetAgeFromFile() { ... } 

};

 

 

 

TEST_F(Examples, Throwing)

{

   Person* personPtr = new Person();

   WHEN_CALLED(personPtr->GetAgeFromFile()).Throw("File doesn't exist");

   

   ASSERT_EQ (-1, personPtr->GetAge());

}

 

Note: You can throw any exception as long as it is actually throw within the code.

 
Note: To catch an exception thrown in an Original behavior of a method on a 64-bit system in your test, use the HandleExceptionInTest flag. For more details, see Setting Default Behavior of a Faked Class.

 


Copyright  Typemock Ltd. 2009-2025.  All Rights Reserved.