package com.isoftstone.ecc.junittest.service;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.lang.ArithmeticException;
import static org.junit.Assert.*;
//import static org.junit.Assert.assertEquals;
//import static org.junit.Assert.assertTrue;
//import static org.junit.Assert.assertFalse;
//import static org.junit.Assert.*;
public class Junit4Demo {
@Before
public void before(){
System.out.println("before is executed once.");
}
@Test
public void test1(){
System.out.println("test1 is executed.");
}
@Test
public void test2(){
System.out.println("test2 is executed.");
}
// @Test
// public int test3(){
// System.out.println("test3 is executed.");
// return 3;
// }
@Test(expected=ArithmeticException.class)
public void test4(){
System.out.println("test4 is executed");
int i = 3/0;
}
@AfterClass
public static void test5(){
System.out.println("test5 @AfterClass:after class is executed once.");
}
@BeforeClass
public static void test6(){
System.out.println("test6 @BeforeClass: before class is executed once.");
}
@Test
public void test7(){
System.out.println("test7 is executed");
// assert 0>8;
// assert 0<8;
int x = 10;
assert x==10:"x==10 success";
// assert x==100:"x==100 failed";
}
@Test
public void test8(){
System.out.println("test8 is executed");
// assertEquals("target 与 result 不相等,中断测试方法,输出 message", "target", "result");
boolean b = Boolean.TRUE;
assertTrue("Result 为true ",b);
// assertFalse("Result false ", b);
Object obj = null;
// assertNull(obj);
// assertNull("obj is not null ", obj);
// assertNotNull("obj is null", obj);
// String str1 = "str1";
// String str2 = new String("str1");
// assertSame("Traget 与 result 不指向/指向 同一内存地址(实例)", str1, str2);
// fail("中断测试方法,输出 message");
}
}
/**
* DbTestJunit4.java
* com.isoftstone.ecc.junittest.service
*
* Function: TODO
*
* ver date author
* ──────────────────────────────────
* Aug 31, 2011 程仁银
*
* Copyright (c) 2011, All Rights Reserved.
*/
package com.isoftstone.ecc.junittest.service;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
// assert.class static class or method
import static org.junit.Assert.assertEquals;
import org.easymock.EasyMock;
import org.easymock.IMocksControl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* ClassName:DbTestJunit4
* Project:
* Company: isoftStone
*
* @author 程仁银
* @version
* @since Ver 1.1
* @Date Aug 31, 2011 12:09:25 PM
* @see
*/
public class DbTestJunit4 {
private DbTest dbTest ;
@Before
public void init(){
//重新初始化对象
//EasyMock.reset(DbTest.class);
dbTest = new DbTest();
}
@After
public void detroy(){
dbTest = null;
}
@Test
public void getCar()throws SQLException{
// 测试数据
int id = 18;
//createMock:创建Mock对象 - 模拟对象之间有依赖关系
IMocksControl control = EasyMock.createControl();
// 创建多个Mock对象时通过IMocksControl管理
Connection conn = control.createMock(Connection.class);
PreparedStatement pst = control.createMock(PreparedStatement.class);
ResultSet rs = control.createMock(ResultSet.class);
//expect:录制信息,即设定Mock对象的预期行为和输出
// 所有Mock对象需要执行的方法都必须录制
String sql = "select * from lock_info where ID = ? ";
EasyMock.expect(conn.prepareStatement(sql)).andReturn(pst).times(1);
pst.setInt(1,id);
EasyMock.expect(pst.executeQuery()).andReturn(rs);
EasyMock.expect(rs.next()).andReturn(true);
//replay:录制完成,切换回放状态
control.replay();
//调用实际的方法
String res = dbTest.getCar(new DBconnect().getConectionByJdbc(),id);
System.out.println("调用实际的方法 res = " + res);
String expect = "success";
assertEquals(res, expect);
//verify:验证
EasyMock.verify();
}
}
分享到:
相关推荐
单元测试框架Junit—— JUnit3.8.1 以及使用JUnit测试的代码demo。
junit4 使用总结 @Test @Test(timeout=毫秒),@Test(expected=异常类) @Ignore @RunWith @BeforeCalss @AfterClass @Before @After 测试套件@RunWith(Suite.class) 参数化设置@RunWith(Parameterized.class)
为了证明对JUnit Jupiter中的assertAll() (即,分组的断言)的支持,而对于JUnit 4中缺少对分组的断言的支持,某些测试有意地失败。其他测试有意地未能证明@SkipOnFailuresInEnclosingClass扩展的功能。
Java测试-演示(JUnit 5)
JUnit 4异常处理演示 JUnit 4处理异常。 参见HelloTest.java 。 mvn test
使用之前需要配置pom.xml ... <scope>test <!-- https://mvnrepository.com/artifact/io.netty/netty-all --> <groupId>io.netty <artifactId>netty-all <version>4.1.50.Final </dependency>
Android 测试演示 - 带有匕首、mockito 的基础 Junit 使用 Dagger 2、Espresso 2 和 Mockito 进行 Android 测试
Android Volley Demo:数据的加载在test包中,使用JUnit测试即可。Activity中是三种图片的加载方式。
class, scannedPackages = " governator.junit.demo " ) public class SampleGovernatorRunnerTest { @Inject private BlogService blogService; @Test public void testExampleBeanInjection () throws ...
它间接引用 junit-4.12 mockito-core-2.15.0 但是官网powermock 1.7.x只支持2.8.0-2.8.9,如果使用默认引入版本,会出现找不到某些方法的异常. 需要自定义mockito-core为相应版本。 ServiceAspect只是为了验证当spy...
<spring-test-version>4.1.6.RELEASE</spring-test-version> <httpclient-version>4.5.1 <zkclient-version>0.1 <sl4j.version>1.7.25 <groupId>com.alibaba</groupId> <artifactId>fastjson ${...
<description>Demo project for Spring Boot <java.version>1.8 <groupId>org.springframework.boot <artifactId>spring-boot-starter <groupId>org.springframework.boot ...
设置为E-PRTR的demo,供大家讨论改写使用。 该应用程序使用 Liquibase 创建和升级数据库,并使用 Thymeleaf 作为模板引擎。 您可以在 src/main/webapp/WEB-INF/thymeleaf/layout.html 找到布局模板。 数据库在内存...
ft-test-factory-demo 该基于Serenity BDD的测试工厂是针对IMD Connect的回归功能测试。 复制自 码头工人 要运行Chrome,请使用 docker-compose up -d 连接到VNC。 密码是secret 。 vnc://127.0.0.1 停止Docker ...
demo for Jax-rs with the Junit test #开发需求 1、实现RS服务 2、生成SWAGGER接口文档 3、实现接口单元测试示例 4、对接口访问过程进行日志记录 #实现方案 REST API实现方式 Spring Boot框架实现方法 1、创建资源...
包含ContentProvider提供访问数据的继承ContentProvider的程序,也有个用junit方式方式provider的应用 得先运行TestCase.java中的testInsert方法初始化创建下数据库 安装contentprovider应用后 其他contentprovider...
Android轻量级MVVM demo, 简单易读易上手: https://gitee.com/aican/mvvm_demo mvvm框架的demo。精简删除google官方demo...mvvm有一个很大的优点就是可以做junit test 测试, 那么理论上我们的所有的逻辑代码,for、i
| --- domains ( simple hello service, junit test ) | --- public-web ( simple web, junit test (mokito)) 网络数据库应用程序 multi-web-demo | --- commons | --- domains ( jpa entities, repository, ...
演示-java-hungnguyendang 带有示例应用程序的演示 TDD 设置gradle 设置mongodb 通过以下方式启动 mongodb:#bin/mongod,使用 mongodb 控制台 #bin/mongo 创建数据库 demojavahungnguyen #使用...
service 集成了dubbo,test目录下有dubbo消费者 Junit单元测试 service-api 接口 utils 工具类,简单封装了okhttp 3.0 web 使用的是spring推荐的模板引擎 thymeleaf ,优点是前后端可以很好的分离,理念界面即原型,...