Skip to content
Advertisement

Mockito and ReloadableResourceBundleMessageSource doesn’t go well together?

I have this simple test, it isn’t even a test as I’m simply trying to mock the messagesource.

I’m getting this error:

org.springframework.context.NoSuchMessageException: No message 
found under code '' for locale 'null'.

Can anyone else verify this behavior?

This is a minimum spring boot skeleton test I set up because ReloadableResourceBundleMessageSource didn’t work in another project, so I thought I’ll just try it in the smallest unit possible.

@RunWith(MockitoJUnitRunner.class)
public class DemoApplicationTests {

    @Test
    public void contextLoads() {

    ReloadableResourceBundleMessageSource messageSource = 
    mock(ReloadableResourceBundleMessageSource.class);
    when(
       messageSource.getMessage(
           anyString(), 
           any(Object[].class), 
           any(Locale.class)
         )
       ).thenReturn("returnValue");

    System.out.println("test");

    } 
}

This is the error I get:

org.springframework.context.NoSuchMessageException: No message 
found under code '' for locale 'null'.

at org.springframework.context.support.AbstractMessageSource.getMessage(
AbstractMessageSource.java:159)
at com.example.DemoApplicationTests.contextLoads(
DemoApplicationTests.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at ....
....
....
....
com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(
JUnit4IdeaTestRunner.java:117)
at com.intellij.rt.execution.junit.JUnitStarter.
prepareStreamsAndStart(
JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(
JUnitStarter.java:74)

It seems to me that it’s not able to mock it properly. I’ve also tried to mock the MessageSource interface to no avail.

Advertisement

Answer

As David Wallace said:

Mockito can’t stub “final” methods, and as ‘getMessage’ is declared final you can’t mock it.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement