Skip to content

Commit 431b268

Browse files
committed
create unit test init
1 parent ee7c769 commit 431b268

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed

pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,19 @@
183183
<version>2.16</version>
184184
</dependency>
185185

186+
<dependency>
187+
<groupId>org.mockito</groupId>
188+
<artifactId>mockito-core</artifactId>
189+
<version>3.3.3</version>
190+
<scope>test</scope>
191+
</dependency>
192+
<dependency>
193+
<groupId>junit</groupId>
194+
<artifactId>junit</artifactId>
195+
<version>4.12</version>
196+
<scope>test</scope>
197+
</dependency>
198+
186199
</dependencies>
187200
</dependencyManagement>
188201

wechaty/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@
8989
<groupId>net.sourceforge.htmlcleaner</groupId>
9090
<artifactId>htmlcleaner</artifactId>
9191
</dependency>
92+
93+
<dependency>
94+
<groupId>org.mockito</groupId>
95+
<artifactId>mockito-core</artifactId>
96+
<scope>test</scope>
97+
</dependency>
98+
<dependency>
99+
<groupId>junit</groupId>
100+
<artifactId>junit</artifactId>
101+
<scope>test</scope>
102+
</dependency>
103+
92104
</dependencies>
93105

94106
<build>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package io.github.wechaty.user
2+
3+
import io.github.wechaty.Wechaty
4+
import io.github.wechaty.util.MockitoHelper
5+
import org.junit.After
6+
import org.junit.Before
7+
import org.junit.Ignore
8+
import org.junit.Test
9+
import org.mockito.ArgumentMatchers
10+
import org.mockito.Mockito
11+
import org.mockito.Mockito.`when`
12+
import org.mockito.Mockito.verify
13+
14+
/**
15+
* @author renxiaoya
16+
* @date 2020-05-29
17+
*/
18+
class RoomTest {
19+
20+
lateinit var mockWechaty: Wechaty
21+
22+
lateinit var room: Room
23+
24+
@Before
25+
fun setUp() {
26+
//TODO(create a mock puppet for unit test)
27+
//these can not fill the inner field in wechaty.To resolve it,I may define a mock puppet for unit test
28+
mockWechaty = Mockito.mock(Wechaty::class.java)
29+
room = Mockito.mock(Room::class.java)
30+
31+
}
32+
33+
@After
34+
fun tearDown() {
35+
36+
}
37+
38+
@Test
39+
// ignore temporary because it doesn't work for now
40+
@Ignore
41+
fun say() {
42+
`when`(room.alias(MockitoHelper.anyObject())).thenReturn("test-alias")
43+
val msgId = "test_msgId"
44+
`when`(mockWechaty.getPuppet().messageSendText(ArgumentMatchers.anyString()
45+
, ArgumentMatchers.anyString()
46+
, ArgumentMatchers.anyList()).get()).thenReturn(msgId)
47+
val msg = Mockito.mock(Message::class.java)
48+
`when`(mockWechaty.messageManager.load(msgId)).thenReturn(msg)
49+
50+
val text = "test-text"
51+
val contact1 = Contact(mockWechaty, "contact1")
52+
val contact2 = Contact(mockWechaty, "contact2")
53+
val contact3 = Contact(mockWechaty, "contact3")
54+
55+
verify(room.say(text, listOf(contact1, contact2, contact3)))
56+
57+
}
58+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.wechaty.util
2+
3+
import org.mockito.Mockito
4+
5+
/**
6+
* A helper for Mockito
7+
*
8+
* @author renxiaoya
9+
* @date 2020-05-29
10+
**/
11+
object MockitoHelper {
12+
fun <T> anyObject(): T {
13+
Mockito.any<T>()
14+
return uninitialized()
15+
}
16+
17+
@Suppress("UNCHECKED_CAST")
18+
fun <T> uninitialized(): T = null as T
19+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mock-maker-inline

0 commit comments

Comments
 (0)