本文原创,转载请注明出处
在上文中,已经简单的解析了junit中test注解的使用方法,今天在进行test测试时,遇到了一个异常,于是想深
入的研究一下。
还原一下今天的异常代码:
@Service public class UserResourceDayStatisticsServiceImpl implements UserResourceDayStatisticsService { @Autowired private UserResourceDayStatisticsMapper dayStatisticsMapper; @Override public UserResourceDayStatistics getResourceDayStatistics(UserResourceDayStatistics dayStatistics) { try { dayStatistics =dayStatisticsMapper.getResourceDayStatistics(dayStatistics); System.out.println("dayStatistics"+dayStatistics); } catch (Exception e) { e.printStackTrace(); } return dayStatistics; } @Test public void test(){ try { UserResourceDayStatistics dayStatistics = new UserResourceDayStatistics(); String userId="1234567890000"; dayStatistics.setUserId(userId); System.out.println(getResourceDayStatistics(dayStatistics)); } catch (Exception e) { e.printStackTrace(); } } }
在本地启动的时候,报以下错误:
java.lang.NullPointerException at com.allcam.bpc.modules.statistics.service.internal.UserResourceDayStatisticsServiceImpl.getResourceDayStatistics(UserResourceDayStatisticsServiceImpl.java:32) at com.allcam.bpc.modules.statistics.service.internal.UserResourceDayStatisticsServiceImpl.test(UserResourceDayStatisticsServiceImpl.java:48) 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.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:678) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
由于该测试项目部分为中间调用部分,不存在web端,测试的时候想了两种测试,一种是在本地测试,
另一种是在web前端调用后台进行测试,代码第一部分为本地测试,碰到该异常之后找了好长时间的问题,一直没有解决
,请教了大神之后,说test注解是需要在spring容器中才能加载出来,在本地测试的时候,加载不出来所需要的context,
于是写了第二种测试,写了一个测试页面,专用调该控制类中方法,测试代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>请求测试界面</title> </head> <body> <form action="dayStatistics/resDayStatistics" id="dayForm"> <div> <input type="submit" value="input查询"> </div> </form> </body> <script type="text/javascript"> function selectForm(){ $('#dayForm').action="dayStatistics/resDayStatistics"; $('#dayForm').submit(); } </script> </html>
在用前台调用测试的时候,调用进controller,然后就可以加载出来,然后就可以运行了