Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
springjunit4classrunner测试用例抛出空指针_Spring_Unit Testing_Junit4 - Fatal编程技术网

springjunit4classrunner测试用例抛出空指针

springjunit4classrunner测试用例抛出空指针,spring,unit-testing,junit4,Spring,Unit Testing,Junit4,我正在使用JUNIT4+Spring并编写了一个测试用例。我连接了一个JDBC模板,并对其进行了手动设置。但结果是空的,当我使用注入的变量时,测试抛出空指针异常。这里怎么了 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:applicationContextTest.xml" }) @TransactionConfiguration(defaultRollback = t

我正在使用JUNIT4+Spring并编写了一个测试用例。我连接了一个JDBC模板,并对其进行了手动设置。但结果是空的,当我使用注入的变量时,测试抛出空指针异常。这里怎么了

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContextTest.xml" })
@TransactionConfiguration(defaultRollback = true)
@Configurable
public class WriterTest {


    private JdbcTemplate utilityJdbcTemplate;

    public void setUtilityJdbcTemplate(JdbcTemplate utilityJdbcTemplate) {
        this.utilityJdbcTemplate = utilityJdbcTemplate;
    }



    @Test
    @Transactional
    @Rollback(true)
    public void testHappyPath() {
        Assert.assertNotNull(utilityJdbcTemplate);

    }
}
此处测试失败,因为utilityJdbcTemplate为null。为什么?

“必须自动连线”:


不确定如何在不查看相关配置或代码的情况下诊断问题;显示的代码没有理由不为空。我自己在这里找到了答案。谢谢你试着帮助我。不,自动连线不工作。没有那么直接。我试过了。答案就在这里。我只是不想删除这个问题,因为它会帮助其他人工作<测试不需要code>@可配置
@Autowired
@Resource
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
一起工作。工作了很多。。很多次。
@Autowired
private JdbcTemplate utilityJdbcTemplate;