NutzCN Logo
问答 单元测试里用service插入没有异常,但数据库里却找不到
发布于 1240天前 作者 wx_7qr0j9qpmienlificb6v 1215 次浏览 复制 上一个帖子 下一个帖子
标签:

单元测试里用service插入没有异常,而且查询的结果没有问题
在调用service新插入时,实体类的自动生成的id也能正常返回
但是为什么数据库里却找不到?

    private static final Log log = Logs.get();
    ProductTypeService productTypeService;
    ProductService productService;
    ProductInventoryService productInventoryService;

    //获取类型的键值对,方便匹配
    private static NutMap typeMap;

    @Before
    public void _init() {
        if (productTypeService == null) {
            productTypeService = ioc.get(ProductTypeServiceImpl.class);
        }
        if (productService == null) {
            productService = ioc.get(ProductServiceImpl.class);
        }
        if (productInventoryService == null) {
            productInventoryService = ioc.get(ProductInventoryServiceImpl.class);
        }
        try {
            Trans.begin();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这是日志:

[DEBUG] 2021-07-01 13:43:28,078 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) - INSERT INTO product_type(id,parentId,path,name,code,location,hasChildren,opBy,opAt,delFlag) VALUES(?,?,?,?,?,?,?,?,?,?) 
    |                                1 |                                2 |        3 |       4 |       5 | 6 |     7 | 8 |          9 |    10 |
    |----------------------------------|----------------------------------|----------|---------|---------|---|-------|--|------------|-------|
    | 44bc2a174a6b4858acbaa2f74097c876 | 53ce3f8da1d743a4bf29fe8977fd3731 | 00010001 | 100彩蝶轩券 | 100彩蝶轩券 | 2 | false |  | 1625118208 | false |
  For example:> "INSERT INTO product_type(id,parentId,path,name,code,location,hasChildren,opBy,opAt,delFlag) VALUES('44bc2a174a6b4858acbaa2f74097c876','53ce3f8da1d743a4bf29fe8977fd3731','00010001','100彩蝶轩券','100彩蝶轩券',2,false,'',1625118208,false) "
[DEBUG] 2021-07-01 13:43:28,200 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) - UPDATE product_type SET hasChildren=?  WHERE id=?
    |    1 |                                2 |
    |------|----------------------------------|
    | true | 53ce3f8da1d743a4bf29fe8977fd3731 |

这是结束后的日志:

  For example:> "INSERT INTO product_inventory(id,product_id,total_number,repair_number,financial_stock,sales_number,order_number,supplier_id,borrow_out_number,borrow_in_number,can_sale_number,so,cases_number,cost_price,stock_amount,projected_cost,tax_price,no_tax_price,opBy,opAt,delFlag) VALUES('ff5e1e10417f481b8c9f5488e74442f8','3786955c19d14dfea2c6856590d8967b',0,0,0,0,0,NULL,0,0,0,0,0,'0','0','0','0','0','',1625118209,false) "
[INFO ] 2021-07-01 13:43:29,908 org.nutz.ioc.impl.NutIoc.depose(NutIoc.java:287) - org.nutz.ioc.impl.NutIoc@232042179 is closing. startup date [21-07-01 13:43:24.524]
[INFO ] 2021-07-01 13:43:29,918 com.alibaba.druid.pool.DruidDataSource.close(DruidDataSource.java:1863) - {dataSource-1} closed
[INFO ] 2021-07-01 13:43:29,918 org.nutz.ioc.impl.NutIoc.depose(NutIoc.java:301) - org.nutz.ioc.impl.NutIoc@232042179 is deposed. startup date [21-07-01 13:43:24.524]

4 回复

补充一下:
1、在单元测试中查数据可以查的出来,只有insert时就没有记录
2、在线上环境中调用service里同一个插入方法是可以正常插入的

是开事务了,去掉 Trans.begin() 即可

添加回复
请先登陆
回到顶部