0

Facing similar issue to the question asked in another thread Null Pointer Exception in Unit test in spock

I have a java class and method that accepts Map (java.util.Map) and returns MyEntity as shown below.

@Service
public class MyService {
     public MyEntity process(Map requestMap) {
          MyEntity retObj = new MyEntity();
          // ...
          return retObj;
     }
}

And I am writing unit test for this method call in Spock like below

class mySpec extends Specification {
    MyService service

    void setup() {
        // ...
        service = new MyService()
        // ...
    }

     def "call process with request map"(){
        given:
        Map requestMap = [
            id : 'd21f7479-8fd0-46c1-b612-1375f3fa3289',
            tenantId : '507f191e810c19729de860ea'
        ] 
        // ... mockObject from repo (not shown for the sake of brevity) 

        when:
        MyEntity foundObject = service.process(requestMap) // Here is where NPE occurs

        then:
        foundObject.getId() == mockObject.getId()
    }
}

The Null pointer exception occurs during when: block

MyEntity foundObject = service.process(requestMap)

Could anyone suggest where I might be going wrong?

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Welcome to SO. Please be advised to learn what an [MCVE](https://stackoverflow.com/help/mcve) is, then edit you question in order to be one. I cannot see what exactly `MyService.process(Map)` does and which objects it uses (especially what it does with the provided map), you do not share a full stack trace, you omit test code which might be connected to the problem you are experiencing. This way all people here can do is speculate, not analyse your problem. So please make it reproducible. Thank you. – kriegaex Mar 12 '20 at 06:45
  • The setup() and "service" initialization looks correct. Have you debugged the process method? Seems like it occurs while executing that method. Can you provide more implementational details on that? – Kekzpanda Mar 26 '20 at 00:56

0 Answers0