junit中test注解测试使用案列解析二
本文原创,转载请注明出处
在上文中,已经简单的解析了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注解测试使用案列解析二的更多相关文章
- junit中test注解测试使用案列解析一
本文原创,转载请注明出处 在写代码的过程中,只想测试部分代码,调试一小段功能有没有通的情况下,可以用该方法: 以下为在项目中测试一个小功能的案例,在此记录一下, /** * <解析查询磁 ...
- Spring MVC注解的一些案列
1. spring MVC-annotation(注解)的配置文件ApplicationContext.xml <?xml version="1.0" encoding=& ...
- python中继承的语法及案列
案例: 1 class Chinese: # 类的创建,类名首字母大写 2 eye = 'black' # 类属性的创建 3 4 def eat(self): # 实例方法创建 5 print('吃饭 ...
- Spring Boot中@ConfigurationProperties注解实现原理源码解析
0. 开源项目推荐 Pepper Metrics是我与同事开发的一个开源工具(https://github.com/zrbcool/pepper-metrics),其通过收集jedis/mybatis ...
- axis1,xfire,jUnit 测试案列+开Web Service开发指南+axis1.jar下载 代码
axis1,xfire,jUnit 测试案列+Web Service开发指南(中).pdf+axis1.jar下载 代码 项目和资源文档+jar 下载:http://download.csdn. ...
- JUnit 4 使用 Java 5 中的注解(annotation)
JUnit 4 使用 Java 5 中的注解(annotation),以下是JUnit 4 常用的几个 annotation 介绍@Before:初始化方法@After:释放资源@Test:测试方法, ...
- Junit中常用的注解说明
Java注解((Annotation)的使用方法是@注解名 ,能通过简单的词语来实现一些功能.在junit中常用的注解有@Test.@Ignore.@BeforeClass.@AfterClass.@ ...
- JUnit中测试异常抛出的方法
最近在做TWU关于TDD的作业,对JUnit中测试异常抛出的方法进行了一些学习和思考. 在进行单元测试的时候有的时候需要测试某一方法是否抛出了正确的异常.例如,我有一个方法,里面对一个List进行读取 ...
- Tensorflow 中(批量)读取数据的案列分析及TFRecord文件的打包与读取
内容概要: 单一数据读取方式: 第一种:slice_input_producer() # 返回值可以直接通过 Session.run([images, labels])查看,且第一个参数必须放在列表中 ...
随机推荐
- 廖威雄: 思维导图:利用__attribute__((section()))构建初始化函数表与Linux内核init的实现
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/juS3Ve/article/details/79049404 本文具体解说了利用__attribut ...
- 常见的SQLALCHEMY列类型.配置选项和关系选项
类型名称 python类型 描述 Integer int 常规整形,通常为32位 SmallInteger int 短整形,通常为16位 BigInteger int或long 精度不受限整形 Flo ...
- 7.7 Models -- Working with Records
Modifying Attributes 1. 一旦一条record被加载,你可以开始改变它的属性.在Ember.js对象中属性的行为就像正常的属性.作出改变就像设置你想要改变的属性一样简单: var ...
- 软件包管理:rpm命令管理-安装升级与卸载
严格区分大小写 卸载命令不许再包的目录下执行.
- Mysql的group by语句
如上图查询结果,因为group by后面的字段是的分组的依据字段,所以select子句中只有是group by后面的字段,或者是聚集函数才有意义.然而mysql居然可以在select子句中出现不在gr ...
- Listener—监听器
什么是监听器? 监听器是Web应用程序事件模型的一部分 监听器的作用? 1:Web应用中某些状态发生改变的时候会产生相应的事件: a)servletContext.HttpSession.Servle ...
- animation效果
添加一个颜色灰渐变的动画效果. <!DOCTYPE html><html lang="en"><head> <meta charset=& ...
- uva11419 二分图--最小覆盖=最大匹配
大白书355 // UVa11419 SAM I AM // Rujia Liu #include <cstdio> #include <cstring> #include & ...
- Intermediate Python for Data Science learning 2 - Histograms
Histograms from:https://campus.datacamp.com/courses/intermediate-python-for-data-science/matplotlib? ...
- Python: 读文件,写文件
读写文件是最常见的IO操作.Python内置了读写文件的函数. 读写文件前,我们先了解一下,在磁盘上读写文件的功能都是有操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求 ...