Spring Boot JUnit 测试 beanFactory.getBean 空指针异常

用户12505474

我有一个功能正在尝试通过 JUnit 进行测试,但我无法找到如何克服这个问题:

在服务中,我有这个声明:

Warning_Log Warning_Log_FRAGMENT = beanFactory.getBean(Warning_Log.class);

我尝试使用 @MockBean 注释在测试类中声明 beanFactory 对象,但我得到一个空指针异常。

最重要的是,我正在测试的函数是私有的,所以我使用反射来访问它。

你知道在junit测试函数中beanFactory(Warning_Log.class)是如何实现的吗?

编辑

服务中的代码如下:

try {
        JSONObject jsonFragmentRequestWarning_Log = new JSONObject();
        jsonFragmentRequestWarning_Log.put("messaggio", "Create Session Blocked [Assertion="+policy.getString("name")+"]|[flag_block_create_session="+gateway.flag_block_create_session+"]|[ApplicationName="+Request.getHeader("ApplicationName")+"]|[ErroreDaRitornare="+erroreDaRitornare+"]");
        jsonFragmentRequestWarning_Log.put("sanitize", false);

        Warning_Log Warning_Log_FRAGMENT = beanFactory.getBean(Warning_Log.class);
        
        String sFragmentResponseWarning_Log = Warning_Log_FRAGMENT.warning_Log(jsonFragmentRequestWarning_Log.toString(), httpHeaders);

        JSONObject jsonFragmentResponseWarning_Log = new JSONObject(sFragmentResponseWarning_Log);

    }

beanFactory 在服务中自动装配,如下所示:

@Autowired
private BeanFactory beanFactory;

编辑 2 这是我在 junit 函数中尝试导致空指针异常的方法:

Field bf = clazz.getClass().getDeclaredField("beanFactory");
bf.setAccessible(true);
bf.set(clazz, beanfactory);

beanfactory 在类的开头声明如下:

@MockBean
private BeanFactory beanfactory;
埃里克·卡尔斯特兰德

假设您有以下课程:

@Service
public class MyClass {

    @Autowired
    private BeanFactory beanFactory;

    /*
     * Some more code
     */
}

然后您想为该类创建一些单元测试,例如:

@RunWith(SpringRunner.class)
public class MyClassTest {

    @MockBean
    private BeanFactory beanFactoryMock;

    @Autowired
    private MyClass myClass;

    @Test
    public void testMyClass() {

        // Since we have annotated our BeanFactory with @MockBean
        // this is now the instance that will be used by our class-under-test
        Mockito.when(beanFactoryMock.getBean(Warning_Log.class))
            .thenReturn(...);
    }

这是一般的想法。如果您有一个受依赖注入影响的字段,则不必使用反射来在单元测试中访问它。MockBean注释将告诉IoC容器来使用你的嘲笑类无论它被注入。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 Spring Boot CRUD 的 Junit 中的空指针异常

使用Spring Boot进行测试会引发空指针异常

Spring Boot控制器测试,空指针异常

Spring Boot Junit测试-ClassNotFoundException

使用Spring Boot Actuator进行的Junit测试给出异常

JUnit执行测试的Spring Boot问题

Spring Boot @value变量的JUnit测试

spring boot starter 邮件空指针异常

Mockito Spring Boot提供了空指针异常

Spring Boot项目空指针异常

执行junit测试后显示空指针异常错误

使用Spring Boot在JUnit测试中的Spring JavaMailSenderImpl上的ClassNotFoundException

Spring JUnit 5测试中的异常

JUNIT空指针异常

Spring Boot Junit测试安全的应用程序

Spring Boot Junit对putMapping的测试始终返回null

如何在Spring Boot JUnit测试中排除* AutoConfiguration类?

Spring Boot:如何使用@Validated注释在JUnit中测试服务?

在Kotlin和JUnit5中测试Spring Boot缓存

如何使用JUnit在Spring Boot中测试服务层的代码?

在Spring Boot的JUnit测试中创建bean时出错

在 Spring Boot 中使用 Junit 5 进行测试失败

如何为 spring boot 编写 Junit 测试用例:

在junit测试中期望自定义异常而不是空指针异常

服务类Spring boot中的空指针异常

静态方法和空指针异常的问题Spring Boot

Spring Boot Data Jpa中的空指针异常

找不到测试-在裸骨Spring Boot Maven项目上运行jUnit 5测试用例时,测试套件为空

使用@ExtendWith(MockitoExtension.class)的Spring Boot JUnit 5测试不起作用-模拟为空,MockitoJUnitRunner正在工作