NutzCN Logo
问答 正常运行能跑,后来直接把service 写成junit测试就一直不行,好像是外面的Dao注入失败,应该怎样写?求指导
发布于 2862天前 作者 qq_7cfafc4f 2408 次浏览 复制 上一个帖子 下一个帖子
标签:

代码
@IocBean
public class AgentTranDelService {
@Inject
AgentTranDelDao agentTranDelDao;
@Inject
SeqDao seqDao;
@Test
public void saveAgentTranDel() throws Exception{
// Map<String,Object> hmap = Json.fromJson(Map.class, obj);
AgentTranDel agentTranDel = new AgentTranDel();
// long id = seqDao.getSeqByTable("AgentTranDel");
Map<String, Object> hmap = new HashMap<String, Object>();
hmap.put("ORDER_NUM","6");
hmap.put("PAY_TYPE",2);
hmap.put("PROFIT_MON",4);
hmap.put("TRAN_RATE",0.004);
hmap.put("TRAN_TYPE",1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
agentTranDel.setCommodity_nm(hmap.get("COMMODITY_NM").toString());
agentTranDel.setId(10);
agentTranDel.setPay_type(hmap.get("PAY_TYPE").toString());
agentTranDel.setProfit_mon(Float.parseFloat(hmap.get("PROFIT_MON").toString()));
agentTranDel.setRem(hmap.get("REM").toString());
agentTranDel.setStat_dt(sdf.format(new Date()));
agentTranDel.setTran_rate(Float.parseFloat(hmap.get("TRAN_RATE").toString()));
agentTranDel.setTran_type(hmap.get("TRAN_TYPE").toString());
try {
Trans.begin();
// String flag = Json.toJson(agentTranDelDao.saveAgentTranDel(agentTranDel));
AgentTranDel agentTranDel2 = agentTranDelDao.saveAgentTranDel(agentTranDel);
Trans.commit();
// return agentTranDel2;
} catch (Exception e) {
e.printStackTrace();
Trans.rollback();
// return null;
} finally {
Trans.close();
}
}
}
错误
ALL Nutz Log via Log4jLogAdapter
2017-02-20 14:49:28 [console] INFO (main) Nutz is licensed under the Apache License, Version 2.0 .
Report bugs : https://github.com/nutzam/nutz/issues
java.lang.NullPointerException
at com.ifs.tenant.service.AgentTranDelService.saveAgentTranDel(AgentTranDelService.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

4 回复

按照上面的写的还是报错,import junit.framework.Assert;这个好像提示过时了。

package com.ifs.test;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import junit.framework.Assert;

import org.junit.Test;
import org.nutz.dao.Dao;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.trans.Trans;

import com.ifs.tenant.biz.AgentTranDelDao;
import com.ifs.tenant.entity.AgentTranDel;
import com.ifs.util.SeqDao;

@IocBean // 必须有
public class SimpleTest extends Assert {
    
    //private static final Log log = Logs.get();
    
    // 跟通常的@Inject完全一样.
    @Inject("refer:$ioc")
    protected Ioc ioc;
    
    @Inject
    protected Dao dao;
    @Inject
	protected AgentTranDelDao agentTranDelDao;
    @Inject
    protected SeqDao seqDao;
    
    @Test
    public void test_user_service_create_user() throws Exception {
    	AgentTranDel agentTranDel = new AgentTranDel();
    	Map<String, Object> hmap = new HashMap<String, Object>();
    	long id = seqDao.getSeqByTable("AgentTranDel");
		hmap.put("ORDER_NUM","6");
		hmap.put("TRAN_COST",1000);
		hmap.put("PAY_TYPE",2);
		hmap.put("PROFIT_MON",4);
		hmap.put("TRAN_RATE",0.004);
		hmap.put("TRAN_TYPE",1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		agentTranDel.setId(10);
		agentTranDel.setOrder_num(hmap.get("ORDER_NUM").toString());
		agentTranDel.setPay_type(hmap.get("PAY_TYPE").toString());
		agentTranDel.setProfit_mon(Float.parseFloat(hmap.get("PROFIT_MON").toString()));
		agentTranDel.setStat_dt(sdf.format(new Date()));
		agentTranDel.setTran_cost(Float.parseFloat(hmap.get("TRAN_COST").toString()));
		agentTranDel.setTran_rate(Float.parseFloat(hmap.get("TRAN_RATE").toString()));
		agentTranDel.setTran_type(hmap.get("TRAN_TYPE").toString());
		try {
		    Trans.begin();
//		    String flag = Json.toJson(agentTranDelDao.saveAgentTranDel(agentTranDel));
		    AgentTranDel agentTranDel2 = agentTranDelDao.saveAgentTranDel(agentTranDel);
			Trans.commit();
//			return agentTranDel2;
		} catch (Exception e) {
			e.printStackTrace();
		   Trans.rollback();
//		   return null;
		} finally {
		  Trans.close();
		}
    }
}

只要dao调用方法就进入这个异常
public InvocationTargetException(Throwable target) {
super((Throwable)null); // Disallow initCause
this.target = target;
}

要有RunWith,仔细看说明

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