Skip to content

Commit 04fed5a

Browse files
committed
增加 工厂方法 测试
1 parent 0d0048b commit 04fed5a

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

factory-method/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535
<modelVersion>4.0.0</modelVersion>
3636

3737
<artifactId>factory-method</artifactId>
38-
38+
<dependencies>
39+
<dependency>
40+
<groupId>junit</groupId>
41+
<artifactId>junit</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
</dependencies>
3945

4046
</project>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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.factory.method;
25+
26+
import org.junit.Test;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
29+
30+
import static org.junit.Assert.assertEquals;
31+
import static org.junit.Assert.assertTrue;
32+
33+
/**
34+
* 测试工厂方法
35+
*/
36+
public class FactoryMethodTest {
37+
38+
private static final Logger LOGGER = LoggerFactory.getLogger(FactoryMethodTest.class);
39+
40+
@Test
41+
public void testWesternHot() {
42+
Cook cook = new WesternCook();
43+
Food food = cook.cookFood(FoodType.HOT);
44+
verifyFood(food, WesternFood.class, FoodType.HOT);
45+
}
46+
47+
@Test
48+
public void testWesternCold() {
49+
Cook cook = new WesternCook();
50+
Food food = cook.cookFood(FoodType.COLD);
51+
verifyFood(food, WesternFood.class, FoodType.COLD);
52+
}
53+
54+
@Test
55+
public void testChineseHot() {
56+
Cook cook = new ChineseCook();
57+
Food food = cook.cookFood(FoodType.HOT);
58+
verifyFood(food, ChineseFood.class, FoodType.HOT);
59+
}
60+
61+
@Test
62+
public void testChineseCold() {
63+
Cook cook = new ChineseCook();
64+
Food food = cook.cookFood(FoodType.COLD);
65+
verifyFood(food, ChineseFood.class, FoodType.COLD);
66+
}
67+
68+
private void verifyFood(Food food, Class<?> clazz, FoodType type) {
69+
assertTrue("食物必须是" + clazz.getName() + "的子类", clazz.isInstance(food));
70+
assertEquals("食物必须是" + type.getName(), type, food.getFoodType());
71+
}
72+
}

0 commit comments

Comments
 (0)