### Question:
PublishMessage extends RetryableMessage { @Override protected int messageLength() { int length = FormatUtil.toMQttString(topic).length; length += (getQos() == QoS.AT_MOST_ONCE) ? 0 : 2; length += data.length; return length; } PublishMessage(String topic, String msg);  PublishMessage(String topic, byte[] data);  PublishMessage(Header header); String getTopic(); byte[] getData(); String getDataAsString();  }### Answer:
@Test public void messageLengthIsCorrect() { PublishMessage msg = new PublishMessage("Topic", "Message"); int expectedLength = (5+2) + (7+2); assertEquals(expectedLength, msg.messageLength()); msg.setQos(QoS.AT_MOST_ONCE); assertEquals(expectedLength, msg.messageLength()); msg.setQos(QoS.AT_LEAST_ONCE); expectedLength += 2; assertEquals(expectedLength, msg.messageLength()); msg.setQos(QoS.EXACTLY_ONCE); assertEquals(expectedLength, msg.messageLength()); }