这几天在摸索Intellij idea,记录一下小知识点。
第一次接触Intellij,并使用Hibernate框架操作mysql数据库,想要在Intellij中使用单元测试,知道需要有Junit,但是发现默认并没有。
怎么解决呢?
》》首先去settings中找到plugins,在其中搜索JUnitGenerator,如果搜索到了,点击安装即可。如果搜不到,可以先下载JUnitGenerator.jar到本地(https://download.csdn.net/download/gengbaolong/10365267),然后
选择”Install plugin from disk”,选中之前下载的jar包即可。(我就是这么做的)
》》现在,可以在要测试的class中,右键,选择JUnit Test下的JUnit 4,这时候会在src目录下自动产生一个测试类,并自动生成测试class文件。
生成的测试文件如:CustomerTest.java文件
public class CustomerTest {
@Before
public void before() throws Exception {
}
@After
public void after() throws Exception {
}
/**
*
* Method: getCust_id()
*
*/
@Test
public void testGetCust_id() throws Exception {
//TODO: Test goes here...
}
/**
*
* Method: setCust_id(Long cust_id)
*
*/
@Test
public void testSetCust_id() throws Exception {
//TODO: Test goes here...
}
/**
*
* Method: getCust_name()
*
*/
@Test
public void testGetCust_name() throws Exception {
//TODO: Test goes here...
}
/**
*
* Method: setCust_name(String cust_name)
*
*/
@Test
public void testSetCust_name() throws Exception {
//TODO: Test goes here...
}
/**
*
* Method: getCust_source()
*
*/
@Test
public void testGetCust_source() throws Exception {
//TODO: Test goes here...
}
/**
*
* Method: setCust_source(String cust_source)
*
*/
@Test
public void testSetCust_source() throws Exception {
//TODO: Test goes here...
}
/**
*
* Method: getCust_industry()
*
*/
@Test
public void testGetCust_industry() throws Exception {
//TODO: Test goes here...
}
/**
*
* Method: setCust_industry(String cust_industry)
*
*/
@Test
public void testSetCust_industry() throws Exception {
//TODO: Test goes here...
}
/**
*
* Method: getCust_level()
*
*/
@Test
public void testGetCust_level() throws Exception {
//TODO: Test goes here...
}
/**
*
* Method: setCust_level(String cust_level)
*
*/
@Test
public void testSetCust_level() throws Exception {
//TODO: Test goes here...
}
/**
*
* Method: getCust_linkman()
*
*/
@Test
public void testGetCust_linkman() throws Exception {
//TODO: Test goes here...
}
/**
*
* Method: setCust_linkman(String cust_linkman)
*
*/
@Test
public void testSetCust_linkman() throws Exception {
//TODO: Test goes here...
}
/**
*
* Method: getCust_phone()
*
*/
@Test
public void testGetCust_phone() throws Exception {
//TODO: Test goes here...
}
/**
*
* Method: setCust_phone(String cust_phone)
*
*/
@Test
public void testSetCust_phone() throws Exception {
//TODO: Test goes here...
}
/**
*
* Method: getCust_mobile()
*
*/
@Test
public void testGetCust_mobile() throws Exception {
//TODO: Test goes here...
}
/**
*
* Method: setCust_mobile(String cust_mobile)
*
*/
@Test
public void testSetCust_mobile() throws Exception {
//TODO: Test goes here...
}
/**
*
* Method: toString()
*
*/
@Test
public void testToString() throws Exception {
//TODO: Test goes here...
}
}
这是自动生成的方法。
我在单元测试时自定义了方法——方法上必须加上@Test注解。
但是现在发现没有@Test这个注解,这时我们需要导入JUnit的jar包。
下载JUnit的jar包(https://download.csdn.net/download/gengbaolong/10365267)
》》然后将junit-4.7.jar放入web目录下的lib下
将添加的jar包”Add as library”即可。
》》我在CustomerTest中自定义的方法上加上@Test注解):
@Test
public void fun1(){//单元测试方法:向数据库中插入一个cust_name值为seven的记录
//创建配置文件对象
Configuration conf = new Configuration().configure();
//根据配置信息,创建SessionFactory
SessionFactory sessionFactory = conf.buildSessionFactory();
//获得Session------表达hibernate框架与数据库之间的连接(会话).session类似于JDBC中的connection对象.
Session session = sessionFactory.openSession();
//开启事务并 获得操作事务的对象
Transaction tx = session.beginTransaction();
//-----------------
Customer customer = new Customer();
customer.setCust_name("seven");
session.save(customer);
//--------------
tx.commit();//提交事务
session.close();//释放资源
sessionFactory.close();
}
然后右键,选择”Run fun1()”
运行结果如图中控制台所示绿条或者预期结果则表示方法执行成功。