Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

How to mock SpringUtils.getBean("beanName") by Mockito.mockStatic(SpringUtils.class)

when I try to mock a static method likes SpringUtils.getBean("BeanName") with Mockito 3.7.7, , and I got a NullPointerException of applicationContext, and the getBean("BeanName") method seems still been executed, Here is my code as fellow:

public  class SpringUtils implements  ApplicationContextAware {
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtils.applicationContext = applicationContext;
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException {
        // NullPointerException here
        return (T) applicationContext.getBean(name);
    }
}

And this is where I use SpringUtils:

public class Aaa{
    public Integer getSpringBean(){
        RedisTemplate<String,Object> redisTemplate = SpringUtils.getBean("abc");
        System.out.println(1234);
        return 5;
    }
}

The test code as fellow:

@Test
public void test(){

        Aaa a = new Aaa();
        try (MockedStatic<SpringUtils> theMock1 = Mockito.mockStatic(SpringUtils.class)) {
            theMock1.when(()->SpringUtils.getBean(Mockito.anyString())).thenReturn(Mockito.mock(RedisTemplate.class));
        }
        Integer x = a.getSpringBean();
        System.out.println(x);
    }

then I got a NullPointerException:

java.lang.NullPointerException
    at com.labs.common.idgenerator.utils.SpringUtils.getBean(SpringUtils.java:42)
    at com.labs.common.idgenerator.config.Aaa.getSpringBean(Aaa.java:24)

so, how to mock SpringUtils.getBean("beanName") with mockito correctly?

Best Regards!

question from:https://stackoverflow.com/questions/65951409/how-to-mock-springutils-getbeanbeanname-by-mockito-mockstaticspringutils-cl

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are using a try-with-resources block to manage your MockedStatic. This means that the MockedStatic object is created when entering this block, but when the block is exited the resource is automatically closed.

Take a look at MockedStatic.close()

Releases this static mock and throws a MockitoException if closed already.

All interactions with stubbed static method must be done when MockedStatic is still active - in the scope of try-with-resources block:


try (MockedStatic<SpringUtils> theMock1 = Mockito.mockStatic(SpringUtils.class)) {
    theMock1.when(()->SpringUtils.getBean(Mockito.anyString()))
            .thenReturn(Mockito.mock(RedisTemplate.class));
    Aaa a = new Aaa();
    Integer x = a.getSpringBean();
    System.out.println(x);
}   

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...