Skip to content

Commit 270a5ce

Browse files
committed
添加 Adapter 测试
1 parent 80a86a7 commit 270a5ce

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

adapter/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
<groupId>junit</groupId>
4141
<artifactId>junit</artifactId>
4242
</dependency>
43+
<dependency>
44+
<groupId>org.mockito</groupId>
45+
<artifactId>mockito-core</artifactId>
46+
<scope>test</scope>
47+
</dependency>
4348
</dependencies>
4449

4550

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* MIT License
3+
* <p>
4+
* Copyright (c) 2017 James
5+
* <p>
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
* <p>
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
* <p>
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package me.zbl.adapter;
25+
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
29+
import java.util.HashMap;
30+
import java.util.Map;
31+
32+
import static org.mockito.Mockito.*;
33+
34+
/**
35+
* 测试适配器
36+
*/
37+
public class TestAdapter {
38+
39+
private Map<String, Object> beans;
40+
static final String CAR_BEAN = "CAR_BEAN";
41+
static final String BUS_BEAN = "BUS_BEAN";
42+
43+
/**
44+
* 在测试执行之前运行,在 bean 映射中注册 bean 对象
45+
*/
46+
@Before
47+
public void setUp() {
48+
beans = new HashMap<>();
49+
50+
BusAdapter busAdapter = spy(new BusAdapter());
51+
beans.put(BUS_BEAN, busAdapter);
52+
53+
Driver driver = new Driver((BusAdapter) beans.get(BUS_BEAN));
54+
beans.put(CAR_BEAN, driver);
55+
}
56+
57+
/**
58+
* 测试适配器
59+
* <p>
60+
* 作用:确定当在一个 Driver 对象(客户端)中调用 drive() 方法时,其实是在调用 Bus 对象中的 run() 方法
61+
* <p>
62+
* verify() 方法的作用是验证某一行为是否发生过
63+
* <p>
64+
* 适配器的作用就是将目标接口类型转换成客户端所需要的类型
65+
*/
66+
@Test
67+
public void testAdapter() {
68+
Car driver = (Car) beans.get(CAR_BEAN);
69+
driver.drive();
70+
71+
Car adapter = (Car) beans.get(BUS_BEAN);
72+
verify(adapter).drive();
73+
}
74+
}

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<slf4j.version>1.7.25</slf4j.version>
3838
<junit.version>4.12</junit.version>
3939
<logback.version>1.1.7</logback.version>
40+
<mockito.version>1.10.19</mockito.version>
4041
</properties>
4142

4243
<modules>
@@ -72,6 +73,12 @@
7273
<version>${junit.version}</version>
7374
<scope>test</scope>
7475
</dependency>
76+
<dependency>
77+
<groupId>org.mockito</groupId>
78+
<artifactId>mockito-core</artifactId>
79+
<version>${mockito.version}</version>
80+
<scope>test</scope>
81+
</dependency>
7582
</dependencies>
7683
</dependencyManagement>
7784
</project>

0 commit comments

Comments
 (0)