测试代码:
package cn.wizzer;
import cn.wizzer.app.cms.modules.models.Cms_news;
import cn.wizzer.app.web.modules.controllers.platform.cms.CmsNewsController;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
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.log.Log;
import org.nutz.log.Logs;
import javax.servlet.http.HttpServletRequest;
import static org.mockito.Mockito.mock;
@RunWith(MyNutTestRunner.class)
@IocBean // 必须有
public class SimpleTest2 extends Assert {
Log log = Logs.get();
//private static final Log log = Logs.get();
// 跟通常的@Inject完全一样.
@Inject("refer:$ioc")
protected Ioc ioc;
@Inject
protected Dao dao;
@Inject
protected CmsNewsController controller;
@BeforeClass
public static void beforeX() {
//这个是必须的。
MockitoAnnotations.initMocks(SimpleTest2.class);
}
@Test
public void test_create_news() {
HttpServletRequest request = mock(HttpServletRequest.class);
Cms_news news = new Cms_news();
news.setTitle("三胖要革命了~");
Object object = controller.addDo(news, request);
log.info("news_obj:" + object.toString());
}
}
controller方法
@At("/addDo")
@Ok("json")
@RequiresPermissions("platform.cms.news.add")
@SLog(tag = "Cms_news", msg = "${args[0].id}")
public Object addDo(@Param("..")Cms_news cmsNews, HttpServletRequest req) {
try {
cmsNews.setId(R.UU16());
cmsNewsService.insert(cmsNews);
return Result.success("system.success");
} catch (Exception e) {
return Result.error("system.error");
}
}
日志(部分):
[DEBUG] 2017-04-06 08:52:25,857 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) - INSERT INTO cms_news(id,title,opBy,opAt,delFlag) VALUES(?,?,?,?,?)
| 1 | 2 | 3 | 4 | 5 |
|----------------------------------|---------|--|------------|-------|
| 928a5557d3f24e8492f2266c21988905 | 三胖要革命了~ | | 1491439945 | false |
For example:> "INSERT INTO cms_news(id,title,opBy,opAt,delFlag) VALUES('928a5557d3f24e8492f2266c21988905','三胖要革命了~','',1491439945,false) "
java.lang.NullPointerException
at cn.wizzer.framework.base.Result.<init>(Result.java:43)
at cn.wizzer.framework.base.Result.error(Result.java:60)
at cn.wizzer.app.web.modules.controllers.platform.cms.CmsNewsController.addDo(CmsNewsController.java:62)
at cn.wizzer.app.web.modules.controllers.platform.cms.CmsNewsController$$NUTZAOP._aop_invoke(CmsNewsController.java:2)
at org.nutz.aop.InterceptorChain.invoke(InterceptorChain.java:77)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:57)
at cn.wizzer.app.web.commons.slog.SLogAopInterceptor.filter(SLogAopInterceptor.java:61)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
at cn.wizzer.app.web.modules.controllers.platform.cms.CmsNewsController$$NUTZAOP.addDo(CmsNewsController.java:1)
at cn.wizzer.SimpleTest2.test_create_news(SimpleTest2.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.nutz.mock.NutTestRunner.runChild(NutTestRunner.java:33)
at org.nutz.mock.NutTestRunner.runChild(NutTestRunner.java:14)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
[INFO ] 2017-04-06 08:52:26,010 org.nutz.ioc.impl.NutIoc.depose(NutIoc.java:255) - org.nutz.ioc.impl.NutIoc@2105078741 is closing. startup date [17-04-06 08:52:14.333]
[DEBUG] 2017-04-06 08:52:26,010 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:108) - Depose object 'cmsNewsServiceImpl' ...
[DEBUG] 2017-04-06 08:52:26,010 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:108) - Depose object 'cmsNewsController' ...
[DEBUG] 2017-04-06 08:52:26,011 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:108) - Depose object 'cacheManager' ...
[DEBUG] 2017-04-06 08:52:26,011 org.nutz.ioc.impl.ScopeContext.clear(ScopeContext.java:108) - Depose object 'cacheProvider' ...
出在框架的类cn.wizzer.framework.base.Result报空指针,不知道怎么解决?
是不是测试的思路有问题?求指点~