How to Create Unit Test for QTcpServer and Mocking QUdpSocket?
Unsolved
General and Desktop
-
I want to create unit tests on some business logic, not any GUI UI code.
I have a class that extends QTcpServer, which uses a QUdpSocket to send messages to another server. What I need to test is the message the server sends to the other server is correct.
How can I create a Mock of this QUdpSocket? I am using Google Test as my testing framework.
my_tcp_server.h
class MyTcpServer : public QTcpServer { Q_OBJECT public: explicit MycpServer(QObject *parent = 0); ~MyTcpServer(); private: qint64 sendMessage(QSslSocket *sslSocket, const char *buff); QUdpSocket socket; };
my_tcp_server.cpp
MyTcpServer::MyTcpServer(QObject *parent) : QTcpServer(parent) { socket.bind(); } qint64 MyTcpServer::sendMessage(QSslSocket *sslSocket, const char *buff) { qint64 returnValue = 0; MessageStruct message; message.value1 =*((unsigned int*) buff); buff+=4; message.value2 =*((unsigned int*) buff); buff+=4; message.value3 =*((unsigned int*) buff); buff; qint64 returnValue = socket.writeDatagram((const char *) &message, sizeof(MessageStruct), QHostAddress(HOST), PORT); }
The message is constructed of the content in the byte buffer coming from the client connected to the QTcpServer.
What I need to test is that this message is correct, but I do not want to send this message to any socket.
-
Hi,
The usual way would be to make QUdpSocket a pointer and add a setter to your class so you can change the QUdpSocket for something else.