本文原创,转载请注明出处

在上文中,已经简单的解析了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,然后就可以加载出来,然后就可以运行了

junit中test注解测试使用案列解析二的更多相关文章

  1. junit中test注解测试使用案列解析一

    本文原创,转载请注明出处 在写代码的过程中,只想测试部分代码,调试一小段功能有没有通的情况下,可以用该方法: 以下为在项目中测试一个小功能的案例,在此记录一下, /**     * <解析查询磁 ...

  2. Spring MVC注解的一些案列

    1.  spring MVC-annotation(注解)的配置文件ApplicationContext.xml <?xml version="1.0" encoding=& ...

  3. python中继承的语法及案列

    案例: 1 class Chinese: # 类的创建,类名首字母大写 2 eye = 'black' # 类属性的创建 3 4 def eat(self): # 实例方法创建 5 print('吃饭 ...

  4. Spring Boot中@ConfigurationProperties注解实现原理源码解析

    0. 开源项目推荐 Pepper Metrics是我与同事开发的一个开源工具(https://github.com/zrbcool/pepper-metrics),其通过收集jedis/mybatis ...

  5. axis1,xfire,jUnit 测试案列+开Web Service开发指南+axis1.jar下载 代码

    axis1,xfire,jUnit 测试案列+Web Service开发指南(中).pdf+axis1.jar下载    代码 项目和资源文档+jar 下载:http://download.csdn. ...

  6. JUnit 4 使用 Java 5 中的注解(annotation)

    JUnit 4 使用 Java 5 中的注解(annotation),以下是JUnit 4 常用的几个 annotation 介绍@Before:初始化方法@After:释放资源@Test:测试方法, ...

  7. Junit中常用的注解说明

    Java注解((Annotation)的使用方法是@注解名 ,能通过简单的词语来实现一些功能.在junit中常用的注解有@Test.@Ignore.@BeforeClass.@AfterClass.@ ...

  8. JUnit中测试异常抛出的方法

    最近在做TWU关于TDD的作业,对JUnit中测试异常抛出的方法进行了一些学习和思考. 在进行单元测试的时候有的时候需要测试某一方法是否抛出了正确的异常.例如,我有一个方法,里面对一个List进行读取 ...

  9. Tensorflow 中(批量)读取数据的案列分析及TFRecord文件的打包与读取

    内容概要: 单一数据读取方式: 第一种:slice_input_producer() # 返回值可以直接通过 Session.run([images, labels])查看,且第一个参数必须放在列表中 ...

随机推荐

  1. android奔溃日期一闪而过

    Android Studio日期崩溃了一闪而过,看不到原因:可以设置No Filters就可以了

  2. HTML状态消息

    HTTP 状态消息 当浏览器从 web 服务器请求服务时,可能会发生错误. 以下列举了有可能会返回的一系列 HTTP 状态消息: 1xx: 信息 消息: 描述: 100 Continue 服务器仅接收 ...

  3. [py]python自省工具

    参考 在日常生活中,自省(introspection)是一种自我检查行为.自省是指对某人自身思想.情绪.动机和行为的检查.伟大的哲学家苏格拉底将生命中的大部分时间用于自我检查,并鼓励他的雅典朋友们也这 ...

  4. python对象反射和函数反射

    python的对象反射功能,经常在编程时使用.相比较其它的编程语言使用非常方便.反射就是用字符串来操作对象或者类,模块中的成员. 一.对象的反射 反射功能的实现,由这4个内置函数来实现(hasattr ...

  5. 【Cocos2dx 3.x Lua】CCParallaxNode使用

    我们知道,当我们移动时,我们会看到离我们越近的物体,会移动的越快,越远的物体,比如远处的山会移动的很慢,而最远处的物体,比如太阳几乎不动,这个现象叫视差,而在游戏中模仿视差,可以让玩家感觉到游戏中的角 ...

  6. [LeetCode] 230. Kth Smallest Element in a BST_Medium tag: Inorder Traversal

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  7. 2.TypeScript 基础入门(二)

    变量类型的那些事 1.基本注解 类型注解使用:TypeAnnotation 语法.类型声明空间中可用的任何内容都可以用作类型注解. const num: number = 123; function ...

  8. discuz $_G变量

    class.core.php中 global $_G;        $_G = array(            'uid' => 0,            'username' => ...

  9. Python tricks(6) -- python代码执行的效率

    python作为一个动态语言, 本身学习曲线比较平滑, 效率相比起来会比c++和java低一些. 脚本语言都是运行时编译的, 这个对于效率的影响是非常大的. 我借用参考1的代码, 加了点代码impor ...

  10. Linux基础命令---bzcat

    bzcat 解压缩被bzip2压缩过的文件,将文件解压到标准输出,此命令只有一个选项-s.该指令对压缩过的二进制文件没有意义,因为二进制文件没有可读性. 此命令的适用范围:RedHat.RHEL.Ub ...