java - 事务教程-测试回滚

标签 java spring transactions dao jdbctemplate

我正在尝试学习如何在java spring中使用事务。我是一个java新手,所以请耐心等待:-)我试图在下面实现的单元测试正在测试: 如果抛出运行时异常则回滚。

我遇到的问题是 java.lang.NullPointerException?

好吧,我已经删除了一些代码以帮助提高可读性

TutorialTest.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class, loader=AnnotationConfigContextLoader.class)
public class TutorialTest {

    @Autowired
    private Dao dao;


    @Test
    public void that_if_a_runtime_exception_is_thrown_transaction_rolledback(){

        User u = new User();
        u.setUsername("FAIL_TEST");
        u.setPhone("0161");
        u.setEmail("FAIL_TEST@gmail.com");
        //dao.addUser(u);  // -- this will insert if uncommented out so I know it works

        OuterService os  = new OuterService();
        os.addUserThrowError(u);

    }
}

OuterService.java

@ContextConfiguration(classes = AppConfig.class, loader=AnnotationConfigContextLoader.class)
public class OuterService {

    @Autowired
    private Dao dao;

    @Transactional
    public void addUserThrowError(User user) throws RuntimeException{

        dao.addUser(user);  // gives me a java.lang.NullPointerException?

        throw new RuntimeException("This should roll back DB entry");
    }

}

Bean 在

中声明

AppConfig.java

@Configuration
@EnableTransactionManagement
@ComponentScan(value = {"com.training.spring.tx.tutorial.dao",
                        "com.training.spring.tx.tutorial.service"})
public class AppConfig {

    public DataSource dataSource() {

        // Create a BasicDataSource object and configure database

        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost/spring_training_tx");
        dataSource.setUsername("training");
        dataSource.setPassword("training");

        return dataSource;
    }

    @Bean
    public DataSourceTransactionManager transactionManager() {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource());
        return transactionManager;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(){
        return new JdbcTemplate(transactionManager().getDataSource());
    }

}

最佳答案

首先,引用javadoc of @ContextConfiguration

@ContextConfiguration defines class-level metadata that is used to determine how to load and configure an ApplicationContext for integration tests.

考虑一下您如何在 OuterService 上使用它。看起来对吗? OuterService 是否用于加载和配置 ApplicationCOntext 进行集成测试?除非我遗漏了一些重要的东西,否则答案是:不。

那么什么是OuterService?这是某种服务。你似乎想用它作为一个 bean 。什么是 bean ? bean 是一个对象,其生命周期由 Spring 管理。这包括 bean 类的实例化、对象的初始化、后处理以及最后的对象销毁。

如果你像这样创建了对象

OuterService os  = new OuterService();

那么Spring不参与。您创建了该对象,但 Spring 无法连接到该对象。因此,您不能期望 Spring Autowiring 其字段

@Autowired
private Dao dao;

由于您尚未初始化该字段,因此它仍为 null,这会导致 NullPointerException

那么我们如何获得一个由 Spring 管理的 OuterService bean?您可以为 OuterService 声明一个 @Bean 方法,或者使用 @Component 或其任何特化注释 OuterService,并且组件扫描它所在的包。然后将该 bean 注入(inject)到使用它的任何其他 bean 中。例如,

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class, loader=AnnotationConfigContextLoader.class)
public class TutorialTest {

    @Autowired
    private Dao dao;

    @Autowired
    private OuterService os;

然后您可以直接使用该变量。

关于java - 事务教程-测试回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23634827/

相关文章:

java - 在 Struts 2 中通过请求传递上下文参数

java - Autowiring 变量的方法为 null

javascript - 使用 Breeze.js 进行非事务性 SaveChanges

java - 运行时发生异常。空 : InvocationTargetException: Connector configured to listen on port 8080 failed to start -> [Help 1]

java - 带有@Transactional 注释的多个事务管理器

.net - PayPal:付款金额为 'Value too long (max length 10)'

java - Android - 空指针异常 : storage == null

java - 我的 RecyclerView 适配器出现错误,填充了 firebase 数据

java - 如何连接两个字符串以显示在从数据库中获取数据的组合框中

java - Spring Web Security 中的自定义方法