MyBatis学习中遇到的一个离谱错误

当junit5测式类类名以Test开头时

        

public class Testadsdasd {


    private SqlSession sqlSession;

    @BeforeEach
    public void init() throws IOException {
        sqlSession = new SqlSessionFactoryBuilder()
                .build(Resources.getResourceAsStream("mybatis-config.xml"))
                .openSession();

    }

    @Test
    public void testQueryOrderById() throws IOException {
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        Order order = mapper.queryOrderById(1);
        System.out.println("order = " + order);

    }

    @AfterEach
    public void closeAll(){

        sqlSession.commit();
        sqlSession.close();
    }
}

在使用了AfterEach或BeforEach注解后,@Test中无法使用sqlSession报错

There are test failures.

Please refer to G:\IdeaProjects\test444maybaits\som\target\surefire-reports for the individual test results.

查找xml中的报错信息后

        说是sqlSession为空

修改类名字后

public class asdasdasdsa {


    private SqlSession sqlSession;

    @BeforeEach
    public void init() throws IOException {
        sqlSession = new SqlSessionFactoryBuilder()
                .build(Resources.getResourceAsStream("mybatis-config.xml"))
                .openSession();

    }

    @Test
    public void testQueryOrderById() throws IOException {
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        Order order = mapper.queryOrderById(1);
        System.out.println("order = " + order);

    }

    @AfterEach
    public void closeAll(){

        sqlSession.commit();
        sqlSession.close();
    }
}

正常运行

order = Order(orderId=1, orderName=o1, customerId=1, customer=Customer(customerId=1, customerName=c01, orders=null))

更离谱的是

把sqlSession的使用放到AfterEach之后

@AfterEach
public void closeAll(){
    OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
    Order order = mapper.queryOrderById(1);
    System.out.println("order = " + order);
    sqlSession.commit();
    sqlSession.close();
}

又正常了 没事了:)

总结

        不知道原因,有大佬知道的话,求解答

        但是知道了junit5.3.1测试类类名不能以Test开头,如果以Test开头,@Test下的内容会被单独隔开,不能使用@BeforeEach写好的东西

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值