These days, I'm groping for Intellij idea to record some small knowledge points.
First contact with Intellij and use Hibernate framework to operate mysql database. If you want to use unit test in Intellij, you know that Junit is needed, but it is not found by default.
How to solve it?
>>First, go to settings and find plugins. Search JUnitGenerator. If you find plugins, click Install. If you can't find it, you can download JUnitGenerator.jar to the local( https://download.csdn.net/download/gengbaolong/10365267 ) and then
Select "Install plugin from disk" and select the previously downloaded jar package. (that's what I did)
>>Now, you can right-click the class to be tested and select JUnit 4 under JUnit Test. At this time, a test class will be automatically generated in src directory and the test class file will be automatically generated.
The generated test file is as follows: CustomerTest.java file
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...
}
}
This is the method of automatic generation.
I customized the method during unit testing - the @ Test annotation must be added to the method.
But now we find that there is no @ Test annotation. At this time, we need to import JUnit's jar package.
Download JUnit's jar package( https://download.csdn.net/download/gengbaolong/10365267)
>>Then put junit-4.7.jar into lib under the web directory
Add the jar package "Add as library".
>>I add @ Test annotation to the customized method in CustomerTest):
@Test
public void fun1(){//Unit test method: insert a record whose cust name value is seven into the database
//Create profile object
Configuration conf = new Configuration().configure();
//Create SessionFactory according to the configuration information
SessionFactory sessionFactory = conf.buildSessionFactory();
//Get session ------ express the connection (session) between the hibernate framework and the database. Session is similar to the connection object in JDBC
Session session = sessionFactory.openSession();
//Open the transaction and get the object to operate the transaction
Transaction tx = session.beginTransaction();
//-----------------
Customer customer = new Customer();
customer.setCust_name("seven");
session.save(customer);
//--------------
tx.commit();//Submission of affairs
session.close();//Release resources
sessionFactory.close();
}
Then right click and select "Run fun1()"
The green bar or expected result of the running result as shown in the console in the figure indicates the successful execution of the method.