建造者模式Builder是一种常用的设计模式,用于构建不同的产品类。
如有以下的Builder
package com.github.mockito.builder;
class Builder{
private String name;
private String address;
public Builder setName(String name){
this.name = name;
return this;
}
public Builder setAddress(String address){
this.address = address;
return this;
}
public String sayHello(){
return "hello";
}
}
以下是一个调用的场景
package com.github.mockito.builder;
public class BuilderDemo {
public Builder builder;
public String sayHello(){
return builder.setName("name").setAddress("address").sayHello();
}
}
建造者模式引人注目的是它标志性的链式方法调用(Fluent API)。
不过它这个也给单元测试造成了一定的麻烦。 先看这个案例
package com.github.mockito.builder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
public class BuilderDemoLegacyTest {
@Mock
Builder builder;
@InjectMocks
BuilderDemo builderDemo;
@BeforeEach
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testSayHelloShouldFail() {
when(builder.sayHello()).thenReturn("hi");
assertThat(builderDemo.sayHello()).isEqualTo("hi");
}
}
这个用例跑失败了
失败的症状是抛出了空指针异常,根本原因是没有正确的测试桩可供使用,因为在调用sayHello时,mock的builder没有被成功匹配。
看来得根据代码依次来进行打桩。如下例,
@Test
public void testSayHelloLegacy() {
when(builder.setName("name")).thenReturn(builder);
when(builder.setAddress("address")).thenReturn(builder);
when(builder.sayHello()).thenReturn("hi");
assertThat(builderDemo.sayHello()).isEqualTo("hi");
}
这回跑通过了
不过问题来了,为了能匹配测试桩,需要逐个对链式调用中的各个中间返回对象进行打桩。如果只是像这个案例的话,也就算了,如果碰到更为复杂的链式方法调用,打桩的代码就会一大片。
还好Mockito从2.0开始为这个问题提供了一个优雅的解决方法。
RETURNS_SELF(new TriesToReturnSelf())
来看一下使用之后的案例
package com.github.mockito.builder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Answers.RETURNS_SELF;
import static org.mockito.Mockito.when;
public class BuilderDemoTest {
@Mock(answer = RETURNS_SELF)
Builder builder;
@InjectMocks
BuilderDemo builderDemo;
@BeforeEach
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testSayHello() {
// when(builder.setName("name")).thenReturn(builder);
// when(builder.setAddress("address")).thenReturn(builder);
when(builder.sayHello()).thenReturn("hi");
assertThat(builderDemo.sayHello()).isEqualTo("hi");
}
}
通过 @Mock(answer = RETURNS_SELF) ,告诉Mockito来返回被mock的测试桩自身,这样就能支持链式方法调用了。
以下是三个用例的执行结果:
感兴趣的读者可以参考Mockito的Answers枚举类,了解各种返回类型。
package org.mockito;
//
public enum Answers implements Answer<Object> {
RETURNS_DEFAULTS(new GloballyConfiguredAnswer()),
RETURNS_SMART_NULLS(new ReturnsSmartNulls()),
RETURNS_MOCKS(new ReturnsMocks()),
RETURNS_DEEP_STUBS(new ReturnsDeepStubs()),
CALLS_REAL_METHODS(new CallsRealMethods()),
RETURNS_SELF(new TriesToReturnSelf());
//
}
据说RETURNS_DEEP_STUBS也有类似效果,感兴趣的读者可以亲自试一试。
关--祝 : 软件测试那些事