本文共 1072 字,大约阅读时间需要 3 分钟。
最近我们想构建一个需要使用外部依赖进行计算的域对象,同时我们希望在测试的时候能够忽略这些依赖。
最开始,我们简单地在域对象中创建依赖,这使得在测试的过程中,不能随意修改依赖的值。
同样,由于外部依赖仅仅只是域对象的计算所需,并非定义域对象的可变状态,我们不应该把依赖通过构造函数传入域对象内部。
最后,我们把域对象定义成内部类,代码如下:
01 | public class FooFactory { |
02 | private final RandomService randomService; |
03 |
04 | public FooFactory(RandomService randomService) { |
05 | this .randomService = randomService; |
06 | } |
07 |
08 | public Foo createFoo(String bar, int baz) { |
09 | return new Foo(bar, baz); |
10 | } |
11 |
12 | class Foo { |
13 | private String bar; |
14 | private int baz; |
15 |
16 | public Foo(String bar, int baz) { |
17 | this .bar = bar; |
18 | this .baz = baz; |
19 | } |
20 |
21 | public int awesomeStuff() { |
22 | int random = randomService.random(bar, baz); |
23 | return random * 3 ; |
24 | } |
25 | } |
26 | } |
测试这段代码的测试用例如下:
01 | public class FooFactoryTest { |
02 |
03 | @Test |
04 | public void createsAFoo() { |
05 | RandomService randomService = mock(RandomService. class ); |
06 | when(randomService.random( "bar" , 12 )).thenReturn( 13 ); |
07 | FooFactory.Foo foo = new FooFactory(randomService).createFoo( "bar" , 12 ); |
08 | assertThat(foo.awesomeStuff(), equalTo( 39 )); |
09 | } |
10 |
11 | } |
代码看似冗余,却合理地解决了测试与外部依赖的解耦问题。