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

在上文中,已经简单的解析了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. requests的post请求:百度翻译

    import json import requests class Trans(object): def __init__(self, juzi): self.juzi = juzi self.bas ...

  2. 12.预处理数据的方法总结(使用sklearn-preprocessing)

    https://blog.csdn.net/sinat_33761963/article/details/53433799

  3. Bellon(多么痛的领悟)

    尼玛  我今天刚刚知道什么是负权回路 任意相连的无向图之间都是回路!!! 囧了一天了,算是看出来了,渣比 A====B 则A能到B且B能到A,这就是一个回路.

  4. [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 ...

  5. EXTJS4扩展实例:如何使用filter查询treepanel

    我们在使用普通的store时,extjs提供了filterBy,filter等多种方法来过滤数据达到查询效果,但在treepanel中的streeStore却没有实现这个查询,于是,就有了这篇文章. ...

  6. Canvas标签基础

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 新浪微博 SAE

    一.云平台中云是指互联网,网络的一种比喻说法,顾名思义,这种平台允许开发者们或是将写好的程序放在“云”里运行,或是使用“云”里提供的服务,或二者皆是. 二.开发类云平台比较: 1.鼻祖 GOOGLE ...

  8. Python: 正则表达式匹配多行,实现多行匹配模式

    1) 点 (.)去匹配任意字符的时候,不能匹配换行符 在这个模式中(?:.|\n)是指定了一个非捕获组(仅仅用来做匹配,部能通过单独捕获或者编号的组) 2) re.DOTALL re.compile( ...

  9. Linux服务器---apache支持SSL

    Apache支持ssl 1.检测是否安装ssl模块,如果没有就安装 [root@localhost cgi-bin]# rpm -qa | grep mod_ssl           //查看是否安 ...

  10. 在notepad++里面使用正则表达式替换掉所有行逗号前面内容

    需求:在notepad++里面使用正则表达式替换掉所有行逗号前面内容,一文本内容如下(只贴一小部分,实际上N多): 级别,層級程序,程式插件,外掛程式鼠标,滑鼠打印,列印打开,開啟博客,部落格联系,聯 ...