当前位置: 首页 > 知识库问答 >
问题:

Spring自动线上的 NPE 表单 TestExecutionListener

欧阳俊捷
2023-03-14

这可能是错误的编码,但任何想法应该如何做到这一点是赞赏。

我有这个类测试类,需要注入许多服务类。由于我不能在@Autowired对象上使用@BeforeClass,所以我使用AbstractTestExecutionListener。一切都按预期工作,但是当我在@Test块上时,所有对象的评估值均为

知道如何解决这个问题吗?

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ProjectConfig.class })
@TestExecutionListeners({ TestClass.class })
public class TestClass extends AbstractTestExecutionListener {

    @Autowired private FirstService firstService;
    // ... other services

    // objects needs to initialise on beforeTestClass and afterTestClass
    private First first;
    // ...

    // objects needs to be initialised on beforeTestMethod and afterTestMethod
    private Third third;
    // ...

    @Override public void beforeTestClass(TestContext testContext) throws Exception {
        testContext.getApplicationContext().getAutowireCapableBeanFactory().autowireBean(this);

        first = firstService.setUp();
    }

    @Override public void beforeTestMethod(TestContext testContext) throws Exception {
        third = thirdService.setup();
    }

    @Test public void testOne() {
        first = someLogicHelper.recompute(first);
        // ...
    }

    // other tests

    @Override public void afterTestMethod(TestContext testContext) throws Exception {
        thirdService.tearDown(third);
    }

    @Override public void afterTestClass(TestContext testContext) throws Exception {
        firstService.tearDown(first);
    }

}

@Service
public class FirstService {
    // logic
}

共有1个答案

谢志用
2023-03-14

首先,让您的测试类实现< code > AbstractTestExecutionListener 不是一个好主意。< code > TestExecutionListener 应在独立类中实现。所以你可能要重新考虑这种方法。

在任何情况下,您当前的配置都会被破坏:您禁用了所有默认的<code>TestExecutionListener</code>实现。

要包含默认值,请尝试以下配置

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ProjectConfig.class)
@TestExecutionListeners(listeners = TestClass.class, mergeMode = MERGE_WITH_DEFAULTS)
public class TestClass extends AbstractTestExecutionListener {
    // ...
}

祝好

Sam(Spring test context框架的作者)

 类似资料:
  • 问题内容: 我有一个接口IMenuItem 然后我有一个接口实现 有什么方法可以仅使用IMenuItem接口从配置类创建MenuItem的多个实例?与@autowired之类的?还可以通过指定构造函数的参数来自动装配吗? 问题答案: 实际上适合这种情况。你可以自动连接特定的类(实现)或使用接口。 考虑以下示例: 现在,你可以根据注释值选择对象的名称,从而选择使用其中一种实现方式 像这样: 要多次创

  • 我有个小问题。如果类是用@component、@service、@controller或@repository注释的,并且我想注入它的依赖项,我需要@autowired吗? 这段代码对我来说非常适用,因为它是UserDeviceService中指定的@Service注释。是因为那个吗?

  • 我有一个带有自动扫描和@Component注释的Spring项目。一些组件需要使用@Autow的注入到不同的bean中。默认情况下,它是否将是作为单例创建的相同组件bean?如果是,如何将同一组件的不同实例注入不同的bean中? 附言:我知道它接近基础,听起来很一般。只是想自己说清楚。 提前致谢

  • 我将JSR 303验证与hibernate validator一起使用,通过在控制器方法上指定@Valid来自动验证字段。验证运行良好。我已经在表单中添加了一个上传字段,并添加了一个@ request param(“file”)作为多文件文件。现在,只有在提交时所有字段都有效,否则我会得到一个404(错误的请求)。如果我删除@Valid注释,我会得到javax . validation . con

  • 在Camel(JBoss Fuse,特别是Spring DSL)中,我试图用Camel简单表达式读取布尔值。 ${body}是一个有效的非空POJO,'fielda'是另一个嵌套POJO,fieldb是一个布尔值。Fielda可以为空,但body不能为空。 当fielda为非null时,我能够读取值而不会出错。 从各种文档来源(Apache Camel Simple、JBoss Fuse)可以看出

  • 那么我的问题是,@javax为什么会这样做。注释。资源工作,但@AutoWired没有工作。为了在Restful控制器上进行测试,我尝试将MappingJackson2HttpMessageConverter注入@Autowired,在启动时,容器未能找到符合条件的bean,即使该类位于路径上。现在,为了解决这个问题,我进入了一个上下文xml文件并添加了bean: 然后在测试类中有成员变量: 然后