junit在idea中的使用(2)--实践篇
目录:(1)普通java项目(2)在web项目中
(1)普通java项目
直接在代码中写上 @Before @Test即可,想执行main方法,直接右击main,选择run as
import org.junit.Before;
import org.junit.Test; public class Test_hdfs {
@Before
public void int_it (){
int i = ;
System.out.println(i+"steps");
}
@Test
public void main (){
System.out.println("hello junit");
} }
(2)在web项目中
1、创建test方法
1.1、新建test目录

1.2、鼠标点击你要测试的-----ctrl+shift+t,创建新的new test.----选择junit4-----自动在java目录下创建测试类

<dependency>
<!--3.0的junit是使用编程的方式来进行测试,而junit4是使用注解的方式来运行junit-->
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version> <!--4.12就是junit4版本-->
<scope>test</scope>
</dependency>
2、测试dao层
UsersDao
package org.logMonitor.dao; import org.apache.ibatis.annotations.Param;
import org.logMonitor.entity.Users; /**
* Created by lenovo on 2018/1/12.
*/
public interface UsersDao {
/**
* 添加一名用户
* @param user
* @return 插入的行数
*/
int addUser(Users user);
/**
* 删除用户
* @param userId 用户id
* @return 如果影响行数>1,代表影响的行数
*/
void deleteUser(long userId);
/**
* 更改用户信息void
* @param user
* @return 如果影响行数>1,代表影响的行数
*/
int updateUser(Users user);
/**
* 根据用户id返回用户实体
* @return
*/
Users queryById(long userId);
}
UsersDaoTest
package org.logMonitor.dao; import org.junit.Test;
import org.junit.runner.RunWith;
import org.logMonitor.entity.Users;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; import static org.junit.Assert.*; /**
* Created by lenovo on 2018/1/13.
*/
//配置spring和junit整合,junit启动时加载springIOC容器
//junit启动时加载springIOC容器
@RunWith(SpringJUnit4ClassRunner.class)
//告诉junit spring的配置文件
@ContextConfiguration("classpath:spring/spring-dao.xml")
public class UsersDaoTest {
//注入Dao实现类依赖,seckillDao已经初始化好并且放在spring容器中,@Resource会去容器中找
@Resource UsersDao usersDao;
@Test
public void addUser() throws Exception {
Users users = new Users();
users.setEmail("123@qq.com");
users.setMobilePhone("123455");
int account=usersDao.addUser(users);
System.out.println(account);
} @Test
public void deleteUser() throws Exception {
usersDao.deleteUser(2);
} @Test
public void updateUser() throws Exception {
Users users = new Users();
users.setId(1);
users.setEmail("123@qq.com");
users.setMobilePhone("123455");
int account=usersDao.updateUser(users);
System.out.println(account);
} @Test
public void queryById() throws Exception {
Users users = usersDao.queryById(1);
System.out.println(users.getId()+"aaaa");
}
}
junit在idea中的使用(2)--实践篇的更多相关文章
- junit在idea中的使用(1)--理论篇
感觉本文前部分配置太过繁琐,大家可以参考我的这篇文章http://www.cnblogs.com/SuMeng/p/8279879.html(junit在IDEA中使用--实践篇),用添加maven ...
- Springboot的日志管理&Springboot整合Junit测试&Springboot中AOP的使用
==============Springboot的日志管理============= springboot无需引入日志的包,springboot默认已经依赖了slf4j.logback.log4j等日 ...
- Linux中磁盘mbr分区——实践篇
Linux中磁盘mbr分区——实践篇 fdisk命令 语法 fdisk(选项)(参数) 选项 -b <分区大小> 指定每个分区的大小 -l 列出分区表信息 -v 显示版本信息 参数 设备文 ...
- Junit 4 测试中使用定时任务操作
难度:测试中执行线程操作 package com.hfepc.job.dataCollection.test; import java.util.Date; import java.util.List ...
- Junit使用过程中需要注意的诡异bug以及处理办法
在开发过程中我们有时会遇到狠多的问题和bug,对于在编译和运行过程中出现的问题很好解决,因为可以在错误日志中得到一定的错误提示信息,从而可以找到一些对应的解决办法.但是有时也会遇到一些比较诡异的问题和 ...
- Junit在SSH中的集成测试
测试Spring容器 在Junit的测试类中,继承AbstractJUnit4SpringContextTests就可以进行Spring容器测试, 例如下面测试用例, @RunWith(SpringJ ...
- Junit测试类中如何调用Http通信
在使用Junit做测试的时候,有时候需要调用Http通信,无论是request还是response或者是session会话,那么在测试类里该如何调用呢,其实很简单,spring给我们提供了三个类 or ...
- 1.如何在JMeter中使用JUnit
您是否需要在测试过程中使用JUnit? 要回答这个问题,我们先来看看单元测试. 单元测试是软件测试生命周期中测试的最低分辨率. 运行单元测试时,需要在应用程序中使用最小的可测试功能,将其与其他代码隔离 ...
- springBoot中使用使用junit测试文件上传,以及文件下载接口编写
本篇文章将介绍如何使junit在springBoot中测试文件的上传,首先先阅读如何在springBoot中进行接口测试. 文件上传操作测试代码 import org.junit.Before; im ...
随机推荐
- 阿里云RDS备份的tar格式包恢复到本地自建数据库
说明 阿里云RDS-mysql数据库是通过percona-Xtrabackup进行备份的,所以恢复时也需要安装该软件. 另外注意的是:你自己下载的MySQL版本要和阿里云上的MySQL版本一致,不然会 ...
- python c example2:pylame2
#include <Python.h> #include <lame.h> //pyton object variables typedef struct{ PyObject_ ...
- Irrelevant Elements UVA - 1635 二项式定理+组合数公式+素数筛+唯一分解定理
/** 题目:Irrelevant Elements UVA - 1635 链接:https://vjudge.net/problem/UVA-1635 题意:給定n,m;題意抽象成(a+b)^(n- ...
- request:getParameter和getAttribute区别
getParameter 是用来接受用post个get方法传递过来的参数的.getAttribute 必须先setAttribute.(1)request.getParameter() 取得是通过容器 ...
- 基于springCloud的分布式架构体系
Spring Cloud作为一套微服务治理的框架,几乎考虑到了微服务治理的方方面面,之前也写过一些关于Spring Cloud文章,主要偏重各组件的使用,本次分享主要解答这两个问题:Spring Cl ...
- 集合Map多对多映射(使用xml文件)
我们可以使用set,bag,map等来映射多对多关系.在这里,我们将使用map来进行多对多映射. 在这种情况下,将创建三个表. 多对多映射示例 我们需要创建以下文件来映射map元素.首先创建一个项目: ...
- 006android初级篇之jni数据类型映射
JNI是Java Native Interface的缩写,它提供了若干的API实现了Java和其他语言的通信(主要是C&C++) 使用JNI的副作用 一旦使用JNI,JAVA程序就丧失了JAV ...
- c++中sin,cos,arcsin等和在C/C++中使用pi (π) 值
先 #include<math.h> 反3角函数有 acos(double),asin(double),atan(double),atan(double,double),返回值 doubl ...
- PRINTDLG 打印对话框操作
typedef struct tagPD { DWORD lStructSize; HWND hwndOwner; HGLOBAL hDevMode; HGLOBAL hDevNames; HDC h ...
- PowerDesigner 建模后如何导入到数据库
from:https://jingyan.baidu.com/article/7f766daf465e9c4101e1d0d5.html 大家都知道PowerDesigner是一个数据库建模工具,但是 ...