0

I am trying to test a class in Java with spock. I am very new to it.

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);
                        });

Now I should test this class and see what happens if the riskProfile that I am getting is there.

I have started with something like this:

     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 class looks like this:

    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);
    

}

When I try to run my test I get:

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)

                                                     
user9347049
  • 1,927
  • 3
  • 27
  • 66
  • What c'tor _does_ `RiskRProfile` have? – cfrick Sep 01 '21 at 10:54
  • Whaat? :) you have a riskProfile class there – user9347049 Sep 01 '21 at 11:04
  • It's incomplete and `final` fields must be set in the c'tor. – cfrick Sep 01 '21 at 11:55
  • Can you give a code example? Sorry I don’t understand what is c’tor? – user9347049 Sep 01 '21 at 12:02
  • constructor. the error is complaining, that there is no c'tor with the arguments you are passing. and your code does not show the c'tor inside RiskProfile. so the most likely problem here is, that you are calling the wrong c'tor. – cfrick Sep 01 '21 at 12:12
  • There is a c'tor in my class you can check a question update. Thats why I dont understend what is it about – user9347049 Sep 01 '21 at 12:58
  • Not only have you added the c'tor to the question, but you also have "fixed" the `new` call in your example to make it actually match. While the error you see there would _perfectly_ match your previous attempt, this error makes no longer sense for the new code. If you still get the same error, try to clean your project. If it's still there make sure you are not actually looking at the wrong place. – cfrick Sep 01 '21 at 13:07
  • Is `RiskProfile` an inner class defined inside of `com.example.ServiceFacade`? The error message indicates that it is, but the source code suggests that it is not. – Jeff Scott Brown Sep 01 '21 at 14:48

1 Answers1

2

You are invoking the following:

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

The first argument to the RiskProfile constructor needs to be a UserId, not a String.

Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47