java - 如何使用 Spock 测试 Java 类?
问题描述
我正在尝试用 spock 测试 Java 中的一个类。我对它很陌生。
public class VerifyUser {
private final ServiceFacade serviceFacade;
//here is constructor
@RequestMapping(name = "verifyUser",
path = "/verifyuser",
method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<VerifyUserResponse> verifyUser(@RequestBody VerifyUserRequest request) {
serviceFacade.getRiskProfile(user.userId.id)
.flatMap(ServiceFacade.RiskProfile::getAffordabilityCheck)
.ifPresent(ac -> {
String rg = ac.getRiskGroup().name();
VerifyUserResponse.VerifyUserResponseBuilder vur = VerifyUserResponse.builder();
vur.attributes.put("RiskGroup", rg);
});
现在我应该测试这个类,看看如果riskProfile
我得到的那个会发生什么。
我从这样的事情开始:
def "should verify user"() {
given:
def userId = "12345"
when:
def res = mvc.perform(post(url)
.content(json)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn()
.response
then: 1 * serviceFacade.getRiskProfile(user.userId.id) >> new ServiceFacade.RiskProfile("userId", 0, Instant.now(), Optional.empty(), Optional.empty())
RiskProfile.java类如下所示:
public class RiskProfile {
public final UserId userId;
public final long version;
public final Instant created;
public final Optional<Instant> lastUpdated;
public final Optional<AffordabilityCheck> affordabilityCheck;
public RiskProfile(UserId userId, long version, Instant created, Optional<Instant> lastUpdated,
Optional<AffordabilityCheck> affordabilityCheck) {
Validation.notNull(userId, "userId can not be null");
Validation.notNull(created, "created can not be null");
Validation.notNull(lastUpdated, "lastUpdated can not be null");
Validation.notNull(affordabilityCheck, "affordabilityCheck can not be null");
this.userId = userId;
this.version = version;
this.created = created;
this.lastUpdated = lastUpdated;
this.affordabilityCheck = affordabilityCheck;
}
public static RiskProfile create(UserId userId) {
return new RiskProfile(userId, 0, Instant.now(), Optional.empty(), Optional.empty());
}
public static RiskProfile create(UserId userId, Optional<AffordabilityCheck> affordabilityCheck) {
return new RiskProfile(userId, 0, Instant.now(), Optional.empty(), affordabilityCheck);
}
当我尝试运行测试时,我得到:
Could not find matching constructor for: com.example.ServiceFacade$RiskProfile(String, String, Optional)
groovy.lang.GroovyRuntimeException: Could not find matching constructor for: com.example.ServiceFacade$RiskProfile(String, String, Optional)
解决方案
您正在调用以下内容:
new ServiceFacade.RiskProfile("userId", 0, Instant.now(), Optional.empty(), Optional.empty())
构造函数的第一个参数必须RiskProfile
是 a UserId
,而不是 a String
。
推荐阅读
- c++ - 从字符串转换为 double 和 int - C++
- docker - ASP .NET Core 在 Heroku 上给出 System.Net.Sockets.SocketException 错误
- c++ - 我们应该在使用 std::bind 之前检查一个不为空的函数吗?
- php - 通过反射类调用 Eloquent 方法,找不到方法错误
- python - 获取 OSError: [WinError 193] %1 不是带有 anaconda 和 jupyter 的有效 Win32 应用程序
- django - 永远在本地主机上使用 django 应用程序
- c# - 无法在 Linux 下使用 .Net Core 等待 Task.Delay
- python - Py2app 应用程序无法使用 tensorflow
- javascript - 使用 HTMLElement.click() 方法时防止 JavaScript 冒泡 DOM
- javascript - Chrome 扩展通过 chrome.runtime.onMessage 传递当前选项卡 url