JUnit单元测试,学习与总结

传统方法,在类里写main方法,调用需要测试的方法。

单元测试:是单一实体(类或方法)的测试。  

约定:

类放在test包中

类名用XXXTest结尾

方法用testMethod命名

@Test:

注解在测试方法上边,

(expected=XXException.class)  用来抛出异常,有异常测试也通过。

(timeout=xxx)用来限制测试时间,超时测试失败。

failures:是指测试失败

error:是测试程序本身出错

@Rollback(false)

在测试方法上面加上这句,测试数据不回滚

Eclipse新建测试类:

项目new>junit test case  (新建测试实例)。

名字,参考约定

class under test 选择要测试的类,下一步选择要测试的方法。

其他注释:

@Ignore: 被忽略的测试方法

@Before: 每一个测试方法之前运行

@After: 每一个测试方法之后运行

@BeforeClass: 所有测试开始之前运行(例如链接数据库,加载配置文件之类)

@AfterClass: 所有测试结束之后运行(关闭资源)

测试多个类或者方法:

run as  选择run configuration选择要测试的方法。

 具体案例测试Controller:

 

package *****;

import java.util.Arrays;
import java.util.List;
import org.aspectj.lang.annotation.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;

import **.entity.***;
import **.service.***;

@RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试  
@WebAppConfiguration
@ContextConfiguration(
		locations = {
		        "classpath:spring-mvc.xml" ,
		        "classpath:spring-hibernate.xml" ,
		        "classpath:spring.xml" 
		})
@Transactional 
public class DryeyeControllerTest {
	@Autowired
	private ****Service ***Service;
	@Test
	public void test方法名() {
		***Service.方法名(1, 3));
		 	
	}

}

测试里的具体情况需要根据具体测试的类的情况而定。

@RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环境。以便在测试开始的时候自动创建Spring的应用上下文。

加载配置文件:

 

@ContextConfiguration(
        locations = {
                "classpath:spring-mvc.xml" ,
                "classpath:spring-hibernate.xml" ,
                "classpath:spring.xml" 
        })

测试Service:

 

package work.dao.impl;
import static org.junit.Assert.*;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class) // 使用junit4进行测试
@ContextConfiguration(locations = { "classpath:spring-mvc.xml", "classpath:spring-hibernate.xml",
		"classpath:spring.xml" })
@Transactional 
public class DryeyeDaoImplTest {
	@Autowired
	private SessionFactory sessionFactory;

	private Session getCurrentSession() {
		return this.sessionFactory.getCurrentSession();
	}

	@Test
	public void testGetTotal() {

		String Hql = "select count(*) from Dryeye";
		Query query = this.getCurrentSession().createQuery(Hql);
		int total = ((Long) query.iterate().next()).intValue();

//     	int total = ((Long) query.iterate().next()).intValue();
		System.out.println(total);
	}

}

 

测试方法和Contorller一样。需要加载配置文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你对Java单元测试不了解或不懂如何书写,可以通过以下步骤来学习和掌握: 1. 了解单元测试的概念和重要性:单元测试是用于验证代码中最小可测试单元(例如方法、类)的正确性和可靠性的测试方法。它有助于发现和纠正代码中的错误,并提高代码质量和可维护性。 2. 学习JUnit框架:JUnit是Java最常用的单元测试框架,学习使用JUnit来编写和运行单元测试。了解JUnit的基本注解(如`@Test`、`@Before`、`@After`等)和断言方法(如`assertEquals`、`assertNotNull`等)的用法。 3. 设置测试环境:为了进行单元测试,需要设置一个独立的测试环境,以避免与生产环境的代码和数据产生冲突。可以使用内存数据库、虚拟机等工具来搭建测试环境。 4. 编写测试用例:根据单元测试的需求和代码逻辑,编写相应的测试用例来验证代码的正确性。测试用例应覆盖尽可能多的代码分支和边界条件。 5. 运行和分析测试结果:运行单元测试并查看测试结果,了解测试通过和失败的情况。对于失败的测试用例,分析错误原因,进行必要的调试和修复。 6. 学习其他工具和技术:除了JUnit外,还有其他许多与单元测试相关的工具和技术,如Mockito、PowerMock等。学习如何使用这些工具和技术来进行更复杂的测试。 7. 实践和总结:通过不断实践和总结经验,逐渐提高单元测试的能力和熟练度。参与开源项目或者与其他开发者进行交流,可以获得更多的实践机会和反馈。 最重要的是,持续学习和实践是掌握单元测试的关键,只有通过实践才能掌握其中的技巧和经验,提升能力和水平。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值