NutzCN Logo
问答 mock 测试mvc问题
发布于 2740天前 作者 shenxiaolei123 2220 次浏览 复制 上一个帖子 下一个帖子
标签:

自己写了个类TestA继承自AbstractMvcTest,测试时报java.lang.NullPointerException
at org.nutz.mock.AbstractMvcTest.before(AbstractMvcTest.java:46)


@IocBean @InjectName public class TestA extends AbstractMvcTest{ @Override protected void initServletConfig() { servletConfig.addInitParameter("modules", "cn.guagua.mobile.MainModule"); } @org.junit.Test public void test(){ request.setPathInfo("/user/login"); request.addParameter("userid", "1"); request.addParameter("password", "10"); try { servlet.service(request, response); System.out.println(response.getAsString()); } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block } } }

出错信息

java.lang.NullPointerException
	at org.nutz.mock.AbstractMvcTest.before(AbstractMvcTest.java:46)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	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.RunBefores.evaluate(RunBefores.java:24)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	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.runners.ParentRunner.run(ParentRunner.java:363)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

出错的源代码 nc = Mvcs.getNutConfig();这一行nc是空的

	@Override
	@Before
	public void before() throws Exception {
		servletContext = Mock.servlet.context();
		servletConfig = new MockServletConfig(servletContext, "nutz");
		initServletConfig();
		servlet = new NutServlet();
		servlet.init(servletConfig);
		session = Mock.servlet.session(servletContext);
		newreq();
		nc = Mvcs.getNutConfig();
		ioc = nc.getIoc();
		injectSelfFields();
		_before();
	}
8 回复

覆盖一下,改成这样试试

  ioc = Mvcs.ctx()..getDefaultIoc();

@wendal 修改成你给的代码为什么源码中的 nc = Mvcs.getNutConfig(); 这是空的呢

我也不知道为啥,晚上回来翻翻代码才知道<(`^´)>

哈哈,又出错了现在调用这个方法报错了,这个方法好像jdk自带的啊。
request.getRemoteAddr()

2017-06-22 16:24:16,673 [main] WARN  org.nutz.mvc.impl.processor.FailProcessor - Error@/user/login :
java.lang.RuntimeException: Not implement yet!
	at org.nutz.lang.Lang.noImplement(Lang.java:109)
	at org.nutz.mock.Mock$1.invoke(Mock.java:118)
	at com.sun.proxy.$Proxy35.getRemoteAddr(Unknown Source)
	at javax.servlet.ServletRequestWrapper.getRemoteAddr(ServletRequestWrapper.java:271)
	at cn.guagua.mobile.util.HttpUtil.getIpAddr(HttpUtil.java:394)
	at cn.guagua.mobile.util.ReadHeaderUtil.readHeader(ReadHeaderUtil.java:38)
	at cn.guagua.mobile.common.CheckSign.checksign(CheckSign.java:55)
	at cn.guagua.mobile.interceptor.SignCheckInterceptor.filter(SignCheckInterceptor.java:45)
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
	at cn.guagua.mobile.interceptor.UserValidityInterceptor.filter(UserValidityInterceptor.java:50)
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
	at cn.guagua.mobile.user.UserModule$$NUTZAOP.login(UserModule.java:1)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:25)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutServlet.service(NutServlet.java:75)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
	at t.b.TestA.test(TestA.java:47)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	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.runners.ParentRunner.run(ParentRunner.java:363)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

不是自带的呢,需要实现的,但之前没调用过,所以没实现这个方法。

要不,你下载一下nutzmore项目,看看哪里不对就改改,发个pull req ^_^

哈哈,原来要自己实现写啊,request.getServerName()这个方法也没实现。汗
nutzmore项目在哪里下呢

https://github.com/nutzam/nutzmore

我的意思是mock这个插件应该实现这些方法,但木有,需要补

明白了有空去研究下

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