4

I have a class which I am trying unit test. I am trying to test it using Mockito, and to resolve the spring injection with mockito I am using Springockito and Springockito-annotations.

@ContextConfiguration(loader = SpringockitoContextLoader.class,
        locations = {"classpath:testApplication-context-EU.xml"})
public class RelatedSearchToHotUrlProcessorTest extends AbstractJUnit4SpringContextTests {

    @Autowired
    RelatedSearchToHotUrlProcessor processor;

    @ReplaceWithMock
    private RestOperations restTemplate;


    @Test
    public void testGetCategoryFromIdWithNoStoredAlias() {
        Taxonomy mockTaxonomy = mock(Taxonomy.class, RETURNS_DEEP_STUBS);
        GraphType.Node mockNode = mock(GraphType.Node.class);
        when(restTemplate.getForObject(anyString(), eq(Taxonomy.class))).thenReturn(mockTaxonomy);
        when(mockTaxonomy
                .getRev()
                .get(0)
                .getCountry()
                .get(0)
                .getGraph()
                .getNodeOrAtom()
                .get(0)).thenReturn(mockNode);
        when(mockNode.getAlias()).thenReturn("mockalias");
        String categoryAlias = processor.getCategoryAliasFromId(13130L);
        assertEquals("mockalias", categoryAlias);
    }
}

If I remove the @ReplaceWithMock and the private RestOperations restTemplate lines then it makes the right call and the value can be validated as correct. However, I want to mock the RestOperations object inside the processor, but using the @ReplaceWithMock makes the restTemplate variable null, causing it to fail. I haven't been able to work out how to isolate this member and mock it.

Xetius
  • 39,461
  • 24
  • 80
  • 118
  • Yes, I am getting a NullPointerException because the restTemplate object is null for the restTemplate.getForObject() call, but I was expecting it to be a mock object – Xetius Sep 03 '12 at 12:38
  • 2
    try using @Autowired to inject – Arasu Sep 03 '12 at 12:59
  • If you define `RelatedSearchToHotUrlProcessorset.setRestTemplate()` you can inject the mock yourself as part of your test, removing any potential issues with the `@ReplaceWithMock` annotation – Brad Sep 04 '12 at 15:27

1 Answers1

2

Was having a similar problem, I found that annotating with @WrapWithSpy or @ReplaceWithMock was not enough. That is to say the field in the test class was null. Adding the @Autowired annotation in addition to the springockito annotation as per Arasu's comment fixed the problem - although it does look weird...

@Autowired
@WrapWithSpy
private SomeBean someBean;
  • 3
    Should note that this is the documented behaviour as per https://bitbucket.org/kubek2k/springockito/wiki/springockito-annotations "If You want to verify some interactions, You can add @Autowired annotation to the field - so it gets injected." – James Kennard Nov 01 '12 at 09:57
  • An issue has been rasied with Springockito to potentially change this behaviour so as using these annotations will always autowire the mock/spy [Automatically autowire WrapWithSpy and ReplaceWithMock annotated fields](https://bitbucket.org/kubek2k/springockito/issue/14/automatically-autowire-wrapwithspy-and) – James Kennard Nov 16 '12 at 09:30
  • @JamesKennard link is dead – 8bitjunkie Mar 21 '18 at 20:36