首页 > 解决方案 > 如何使用 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)

                                                     

标签: javatestinggroovyspock

解决方案


您正在调用以下内容:

new ServiceFacade.RiskProfile("userId", 0, Instant.now(), Optional.empty(), Optional.empty())  

构造函数的第一个参数必须RiskProfile是 a UserId,而不是 a String


推荐阅读