55 "time"
66
77 mqtt "github.com/eclipse/paho.mqtt.golang"
8+ "github.com/stretchr/testify/assert"
9+ "github.com/stretchr/testify/require"
810)
911
1012func TestConnection (t * testing.T ) {
@@ -15,9 +17,7 @@ func TestConnection(t *testing.T) {
1517 WithUsernameAndPassword ("test" , "pwd" )
1618
1719 brokerOpt , err := hostConfig .Build ()
18- if err != nil {
19- t .Fatal (err )
20- }
20+ require .NoError (t , err )
2121
2222 client := NewClient (
2323 WithClientId ("test" ),
@@ -26,21 +26,13 @@ func TestConnection(t *testing.T) {
2626
2727 mockClientImpl := & mockClient {}
2828 err = client .connect (mockClientImpl )
29- if err != nil {
30- t .Fatal (err )
31- }
29+ require .NoError (t , err )
3230
3331 err = client .Disconnect ()
34- if err != nil {
35- t .Fatal (err )
36- }
32+ require .NoError (t , err )
3733
38- if mockClientImpl .cntConnect != 1 {
39- t .Fatal ("Wrong connect count: " , mockClientImpl .cntConnect , " should be " , 1 )
40- }
41- if mockClientImpl .cntDisconnect != 1 {
42- t .Fatal ("Wrong disconnect count: " , mockClientImpl .cntDisconnect , " should be " , 1 )
43- }
34+ assert .Equal (t , 1 , mockClientImpl .cntConnect )
35+ assert .Equal (t , 1 , mockClientImpl .cntDisconnect )
4436}
4537
4638func TestPublish (t * testing.T ) {
@@ -50,9 +42,7 @@ func TestPublish(t *testing.T) {
5042 WithProtocol (MqttTcp )
5143
5244 brokerOpt , err := hostConfig .Build ()
53- if err != nil {
54- t .Fatal (err )
55- }
45+ require .NoError (t , err )
5646
5747 client := NewClient (
5848 WithClientId ("test" ),
@@ -61,29 +51,19 @@ func TestPublish(t *testing.T) {
6151
6252 mockClientImpl := & mockClient {}
6353 err = client .connect (mockClientImpl )
64- if err != nil {
65- t .Fatal (err )
66- }
54+ require .NoError (t , err )
6755
6856 client .Publish (& Message {
6957 Topic : "/test/testMessage" ,
7058 Value : "{'test': 2}" ,
7159 })
7260
7361 err = client .Disconnect ()
74- if err != nil {
75- t .Fatal (err )
76- }
77-
78- if mockClientImpl .cntConnect != 1 {
79- t .Fatal ("Wrong connect count: " , mockClientImpl .cntConnect , " should be " , 1 )
80- }
81- if mockClientImpl .cntDisconnect != 1 {
82- t .Fatal ("Wrong disconnect count: " , mockClientImpl .cntDisconnect , " should be " , 1 )
83- }
84- if mockClientImpl .cntPublish != 1 {
85- t .Fatal ("Wrong publish count: " , mockClientImpl .cntPublish , " should be " , 1 )
86- }
62+ require .NoError (t , err )
63+
64+ assert .Equal (t , 1 , mockClientImpl .cntConnect )
65+ assert .Equal (t , 1 , mockClientImpl .cntDisconnect )
66+ assert .Equal (t , 1 , mockClientImpl .cntPublish )
8767}
8868
8969type mockClient struct {
0 commit comments