目录:(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)--实践篇的更多相关文章

  1. junit在idea中的使用(1)--理论篇

     感觉本文前部分配置太过繁琐,大家可以参考我的这篇文章http://www.cnblogs.com/SuMeng/p/8279879.html(junit在IDEA中使用--实践篇),用添加maven ...

  2. Springboot的日志管理&Springboot整合Junit测试&Springboot中AOP的使用

    ==============Springboot的日志管理============= springboot无需引入日志的包,springboot默认已经依赖了slf4j.logback.log4j等日 ...

  3. Linux中磁盘mbr分区——实践篇

    Linux中磁盘mbr分区——实践篇 fdisk命令 语法 fdisk(选项)(参数) 选项 -b <分区大小> 指定每个分区的大小 -l 列出分区表信息 -v 显示版本信息 参数 设备文 ...

  4. Junit 4 测试中使用定时任务操作

    难度:测试中执行线程操作 package com.hfepc.job.dataCollection.test; import java.util.Date; import java.util.List ...

  5. Junit使用过程中需要注意的诡异bug以及处理办法

    在开发过程中我们有时会遇到狠多的问题和bug,对于在编译和运行过程中出现的问题很好解决,因为可以在错误日志中得到一定的错误提示信息,从而可以找到一些对应的解决办法.但是有时也会遇到一些比较诡异的问题和 ...

  6. Junit在SSH中的集成测试

    测试Spring容器 在Junit的测试类中,继承AbstractJUnit4SpringContextTests就可以进行Spring容器测试, 例如下面测试用例, @RunWith(SpringJ ...

  7. Junit测试类中如何调用Http通信

    在使用Junit做测试的时候,有时候需要调用Http通信,无论是request还是response或者是session会话,那么在测试类里该如何调用呢,其实很简单,spring给我们提供了三个类 or ...

  8. 1.如何在JMeter中使用JUnit

    您是否需要在测试过程中使用JUnit? 要回答这个问题,我们先来看看单元测试. 单元测试是软件测试生命周期中测试的最低分辨率. 运行单元测试时,需要在应用程序中使用最小的可测试功能,将其与其他代码隔离 ...

  9. springBoot中使用使用junit测试文件上传,以及文件下载接口编写

    本篇文章将介绍如何使junit在springBoot中测试文件的上传,首先先阅读如何在springBoot中进行接口测试. 文件上传操作测试代码 import org.junit.Before; im ...

随机推荐

  1. Django学习之URLconf

    Django处理request的步骤: 1.确定根URLconf 2.载入urls.py,找到变量urlpatterns,urlpatterns是django.conf.urls.url()的实例对象 ...

  2. linux下独立core2.1部署发布过程

    1.vs2017独立发布.linux-64 2.winscp上传到当前用户下面.ubuntu tsl64来讲 3.超级用户sudo su 4.zip解压缩 uzip xxx.zip -d ../../ ...

  3. 【Unity】基于MVC模式的背包系统 UGUI实现

    前言 本文基于MVC模式,用UGUI初步实现了背包系统. Control层包括了点击和拖拽两种逻辑. 博文首发:http://blog.csdn.net/duzixi 下载地址:https://git ...

  4. idea Plugin "Maven Integration Extension" was not loaded: required plugin "Maven Integration" is disabled

    由于自己运行了eclipse maven及idea maven 同时操作,可能产生了以上错误.既: idea  Plugin "Maven Integration Extension&quo ...

  5. 构造三层时报错“程序 “D:\MyTest\....”不包含适合于入口点的静态"Main"方法”

    错误 1 程序“D:\MyTest\EBookShop\Model\obj\x86\Debug\Model.exe”不包含适合于入口点的静态“Main”方法 原因:原来创建项目的时候,用的是“空项目” ...

  6. hdu1754(splay tree 单点更新,成段查询)

    题意就是简单的点更新,成段查询. splay tree 果真是常数比较大的log(n)操作. 比线段树还慢了这么多. // // main.cpp // splay // // Created by ...

  7. (转)gethostbyname() -- 用域名或主机名获取IP地址

    struct hostent *gethostbyname(const char *name); 这个函数的传入值是域名或者主机名,例如"www.google.cn"等等.传出值, ...

  8. 《从零开始学Swift》学习笔记(Day 50)——扩展计算属性、方法

    原创文章,欢迎转载.转载请注明:关东升的博客 可以在原始类型上扩展计算属性,包括实例计算属性和静态计算属性.添加计算属性的定义,与普通的计算属性的定义是一样的. 实例计算属性示例:在网络编程时,为了减 ...

  9. proguard-project.txt和project.properties混淆代码

     [转]利用android proguard混淆代码 防止反编译,优化代码 网上虽然有很多相关博客,不过貌似都不是最新版的..于是百度+谷歌+github上的开源demo,终于成功的配置了androi ...

  10. Consul文档收藏

    英文:https://www.consul.io/intro/getting-started/install.html 中文:http://www.liangxiansen.cn/2017/04/06 ...