0
public class Foo {

   public A getSomething() {
       Properties file = loadProperties().getProperty(something);
   }

   public static Properties loadProperties() {
    
       Properties properties = new Properties();
       properties.load(Foo.class.getClassLoader().getResourceAsStream("id"));
    }
     
       return properties;
}

What I did is:

@InjectMocks
Foo foo = new Foo();

@Test
public void test() {
   MockitoAnnotations.initMocks(this);
   Properties properties = Mockito.mock(Properties.class);
   Mockito.doNothing().when(properties).load(new FileInputStream("application.properties")

   foo.getSomething();
 }

It throws null pointer exception in properties.load method. Any help would be appreciated.

Thank you.

  • The problem is that `foo` isn’t using your mocked instance, it’s creating a new one. I would refactor to allow an existing `Properties` instance to be passed to the constructor of `Foo` and avoid the use of mocks entirely For this test. – Tim Moore Oct 15 '22 at 01:01
  • You could also create a test resource file that would be loaded into the `Properties` object. – Jonasz Oct 15 '22 at 08:35
  • Does this answer your question? [Why is my class not using my mock in unit test?](https://stackoverflow.com/questions/74027324/why-is-my-class-not-using-my-mock-in-unit-test) – knittl Oct 15 '22 at 10:06

0 Answers0