Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito - thenReturn always returns null object

Tags:

java

null

mockito

I'm trying to implement Mockito to test a particular method but the .thenReturn(...) seems to always be returning a null object instead of what I intended:

CUT:

public class TestClassFacade {    // injected via Spring   private InterfaceBP bpService;    public void setBpService(InterfaceBP bpService) {        this.bpService = bpService;   }    public TestVO getTestData(String testString) throws Exception {      BPRequestVO bpRequestVO = new BPRequestVO();      bpRequestVO.setGroupNumber(testString) ;     bpRequestVO.setProductType("ALL") ;                bpRequestVO.setProfileType("Required - TEST") ;      IBPServiceResponse serviceResponse = bpService.getProduct(bpRequestVO);  //PROBLEM      if (serviceResponse.getMessage().equalsIgnoreCase("BOB")) {          throw new Exception();      } else {          TestVO testVO = new TestVO();     }      return testVO;   }  } 

Spring Configuration:

<bean id="testClass" class="com.foo.TestClassFacade">     <property name="bpService" ref="bpService" />  </bean>  <bean id="bpService" class="class.cloud.BPService" /> 

Mockito Test Method:

@RunWith(MockitoJUnitRunner.class) public class BaseTest {      @Mock BPService mockBPService;     @InjectMocks TestClassFacade mockTestClassFacade;      private String testString = null;     private BPRequestVO someBPRequestVO = new BPRequestVO();     private IBPServiceResponse invalidServiceResponse = new BPServiceResponse();      @Test (expected = Exception.class)     public void getBPData_bobStatusCode_shouldThrowException() throws Exception {          invalidServiceResponse.setMessage("BOB");          someBPRequestVO.setGroupNumber(null);         someBPRequestVO.setProductType("ALL");         someBPRequestVO.setProfileType("Required - TEST");          System.out.println("1: " + someBPRequestVO.getGroupNumber());         System.out.println("2: " + someBPRequestVO.getProductType());         System.out.println("3: " + someBPRequestVO.getProfileType());         System.out.println("4: " + someBPRequestVO.getEffectiveDate());          when(mockBPService.getProduct(someBPRequestVO)).thenReturn(invalidServiceResponse);          mockTestClassFacade.getTestData(testString);          verify(mockBPService).getProduct(someBPRequestVO);     } } 

System output:

1: null 2: ALL 3: Required - TEST 4: null 

What's happening here is that when I run the test the serviceResponse object is null on the line in the CUT marked with //PROBLEM above. My desire is to have that object be populated with my "invalidServiceResponse" object from my test method. Judging from the output of my System.out.println's it appears that my bpRequestVO matches my someBPRequestVO in content.

Could some one show me what I'm missing here?

Thanks for your time!

like image 419
risingTide Avatar asked Oct 31 '14 17:10

risingTide


People also ask

Can Mockito return null?

By default, any instance method of the mock instance returns null . The when , thenReturn , thenAnswer , and thenThrow APIs provide a stubbing mechanism to override this behavior. Once stubbed, the method will always return stubbed value regardless of how many times it is called.

What does thenReturn do in Mockito?

The thenReturn() methods lets you define the return value when a particular method of the mocked object is been called.

Does Mockito any match null?

Since Mockito any(Class) and anyInt family matchers perform a type check, thus they won't match null arguments.

What is the difference between doReturn and thenReturn?

One thing that when/thenReturn gives you, that doReturn/when doesn't, is type-checking of the value that you're returning, at compile time. However, I believe this is of almost no value - if you've got the type wrong, you'll find out as soon as you run your test. I strongly recommend only using doReturn/when .


1 Answers

Instead of creating a equals method in your BPRequestVO class you can create a mock argument with "any(YourObject.class)" like this:

when(mockBPService.getProduct(any(BPRequestVO.class))).thenReturn(invalidServiceResponse); 
like image 181
Victor Godoy Avatar answered Oct 10 '22 13:10

Victor Godoy