junit4 @test DEMO

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();
	}
}

你可能感兴趣的:(JUnit4)