Reputation: 3625
I have a test on a code part that needs to throw an exception, but using BOOST_CHECK_THROW
still crashes the test. Here is the test:
#include <boost/test/unit_test.hpp>
#include "tools/CQueueMessage.hpp"
class TestMessage
{
public:
std::string m_message1;
std::string m_message2;
std::string m_messageEmpty;
std::string m_messageEmptyJson;
TestMessage()
: m_message1("{\"photo\":{\"name\":\"pic\",\"extension\":\"jpg\"}}"),
m_message2("{\"photo\":{\"name\":\"pic\",\"extension\":\"png\"}}"),
m_messageEmpty(""), m_messageEmptyJson("{}") {}
~TestMessage() {}
};
BOOST_FIXTURE_TEST_CASE(message2send, TestMessage)
{
QueueMessage qmsg1(m_message1);
BOOST_CHECK_EQUAL(qmsg1.messageToBeSent(), "{\"photo\":{\"name\":\"pic\",\"extension\":\"jpg\"}}");
QueueMessage qmsg2(m_message2);
BOOST_CHECK_EQUAL(qmsg2.messageToBeSent(), "{\"photo\":{\"name\":\"pic\",\"extension\":\"png\"}}");
BOOST_CHECK_THROW(QueueMessage qmsg3(m_messageEmpty), QueueMessageException)
// QueueMessage qmsg4(m_messageEmptyJson);
}
The QueueMessage
class constructor is throwing a QueueMessageException
if the message is empty or if it is an empty json. My problem is that this test is printing:
Running 1 test case...
unknown location(0): fatal error in "message2send": std::exception: Bad message format
*** 1 failure detected in test suite "main"
*** Exited with return code: 201 ***
How can I verify that the exception is thrown?
This is the constructor:
QueueMessage(CString& messageIn)
{
std::stringstream ss;
ss << messageIn;
PTree pt;
json::read_json(ss, pt);
std::string photo = pt.get< std::string >("photo");
if (photo.empty())
{
throw QueueMessageException("Bad message format"); // in debugging it arrives here
}
}
Upvotes: 2
Views: 3003
Reputation: 96241
My psychic debugging powers tell me that your constructor is NOT in fact throwing QueueMessageException
. It looks like it (through the function message2send
) is throwing std::exception
or a different child of it.
Upvotes: 2