简体   繁体   English

Mockito 和 Javax 注释 @PostConstruct

[英]Mockito and Javax annotation @PostConstruct

I'm writing a Java unit test using JUnit Jupiter for a class which has a method with a javax.annotation.PostConstruct annotation.我正在使用 JUnit Jupiter 为一个类编写 Java 单元测试,该类具有带有javax.annotation.PostConstruct注释的方法。

I want to test a method in the class but am hitting an issue where the @PostConstruct method is always called once the object is created (unsurprisingly).我想测试类中的一个方法,但遇到了一个问题,即一旦创建对象,@ @PostConstruct方法总是被调用(不出所料)。 The Mockito mock object I've created to be used in the @PostConstruct method isn't providing the results I require.我创建的用于@PostConstruct方法的 Mockito 模拟对象没有提供我需要的结果。

Here's the method that is being called when the object is created:这是创建对象时调用的方法:

@Service
@Slf4j
public class MyService {
    @Inject
    private MyRepository myRepository;

    private Integer myId;

    @PostConstruct
    public void postInit() {
        MyObject myObj = myRepository.findByName("FOO");
        myId = myObj.getId();
    }

    public void myMethod() {
        ... code to be tested here...
    }
}

My JUnit test class contains the following:我的 JUnit 测试类包含以下内容:

@ExtendWith(SpringExtension.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class MyServiceTest {
    private static final Integer ID = 0;

    @Test
    public void myTest() {
        // Given.
        MyObject myObj = new MyObject();
        myObj.setId(ID);

        MyRepository myRepository = Mockito.mock(MyRepository.class);
        Mockito.when(myRepository.findByName(anyString())).thenReturn(myObj);

        // Test runs here.
        myService.myMethod()
    }
}

When I run the unit test, I can see that the mock has been instantiated via my IDE.当我运行单元测试时,我可以看到模拟已通过我的 IDE 实例化。 However, it always returns a NULL object when the findByName("FOO") method is called, even though it should return a correct instance of MyObject .然而,当调用findByName("FOO")方法时,它总是返回一个 NULL 对象,即使它应该返回一个正确的MyObject实例。

As such, the unit test fails with a NullPointerException because myObj is null.因此,单元测试失败并返回 NullPointerException,因为myObj为 null。

Is there anything obviously incorrect that I'm doing here>?我在这里做的有什么明显不正确的>?

Thanks in advance for any assistance.在此先感谢您的帮助。

I've modified your test slightly, creating MyService via Mockito and making myMethod return the id:我稍微修改了您的测试,通过Mockito创建MyService并使myMethod返回 id:

@ExtendWith(SpringExtension.class)
class MyServiceTest {
    private static final Integer ID = 42;

    @InjectMocks
    MyService myService;

    @Test
    public void myTest() {
        // Given.
        MyObject myObj = new MyObject();
        myObj.setId(ID);

        MyRepository myRepository = Mockito.mock(MyRepository.class);
        Mockito.when(myRepository.findByName(anyString())).thenReturn(myObj);

        // Test runs here.
        assertThat(myService.myMethod()).isEqualTo(ID);
    }
}

When I run this, I get:当我运行这个时,我得到:

java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "this.myId" is null

    at MyService.myMethod(MyService.java:24)
    at MyServiceTest.myTest(MyServiceTest.java:28)
    ...

So clearly our mocking of findByName hasn't worked.很明显,我们对findByName的嘲笑没有奏效。

That's because myRepository is just a local variable in our test -- there's nothing to get it injected into MyService .那是因为myRepository在我们的测试中只是一个局部变量——没有什么可以将它注入MyService

One way to fix this is not to use spring test infrastructure, just plain Mockito.解决此问题的一种方法是不使用 spring 测试基础架构,而只是使用普通的 Mockito。 We use the standard @Mock/@InjectMocks annotations to create our mock and the object we are testing.我们使用标准的@Mock/@InjectMocks 注释来创建我们的模拟和我们正在测试的对象。 The drawback here is that we must remember to call postInit ourselves:这里的缺点是我们必须记住自己调用postInit

@ExtendWith(MockitoExtension.class)
class MyServiceTest {
    private static final Integer ID = 42;

    @Mock
    MyRepository myRepository;

    @InjectMocks
    MyService myService;

    @Test
    public void myTest() {
        // Given.
        MyObject myObj = new MyObject();
        myObj.setId(ID);

        Mockito.when(myRepository.findByName(anyString())).thenReturn(myObj);
        myService.postInit();
        // Test runs here.
        assertThat(myService.myMethod()).isEqualTo(ID);
    }
}

To use Spring to call postInit you can do this: (I'm not sure if there's a more idiomatic way to create the mock MyRepository instance)要使用 Spring 调用 postInit,您可以这样做:(我不确定是否有更惯用的方法来创建模拟 MyRepository 实例)

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {MyService.class})
@ContextConfiguration(classes = {MyServiceTest.Configuration.class})
class MyServiceTest {
    @TestConfiguration
    static class Configuration {
        @Bean
        public MyRepository myRepository() {
            MyRepository myRepository = mock(MyRepository.class);
            MyObject myObj = new MyObject();
            myObj.setId(ID);
            Mockito.when(myRepository.findByName(anyString())).thenReturn(myObj);
            return myRepository;
        }
    }
    private static final Integer ID = 42;
    
    @Autowired
    MyService myService;
    
    @Test
    public void myTest() {
        assertThat(myService.myMethod()).isEqualTo(ID);
    }
}

One way to avoid using @SpringBootTest is to create the MyService instance in our test configuration (I've converted it to constructor injection too):避免使用@SpringBootTest一种方法是在我们的测试配置中创建MyService实例(我也将其转换为构造函数注入):

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {MyServiceTest.Configuration.class})
class MyServiceTest {
    @TestConfiguration
    static class Configuration {
        @Bean
        public MyRepository myRepository() {
            MyRepository myRepository = mock(MyRepository.class);
            MyObject myObj = new MyObject();
            myObj.setId(ID);
            Mockito.when(myRepository.findByName(anyString())).thenReturn(myObj);
            return myRepository;
        }

        @Bean
        public MyService myService(MyRepository myRepository) {
            return new MyService(myRepository);
        }
    }
    private static final Integer ID = 42;

    @Autowired
    MyService myService;

    @Test
    public void myTest() {
        assertThat(myService.myMethod()).isEqualTo(ID);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM