Test the CustomerDao as an example

public class CustomerDAO { public boolean save(Customer customer) { List<Object> args = new ArrayList<>(); args.add(customer.getName()); args.add(customer.getRealAge()); args.add(customer.getHeight()); String sql; Integer id = customer.getId(); if ( id == null || id < 1) { sql = "INSERT INTO customer(name, real_age, height) VALUES (? ,? ,?) "; } else { sql = "UPDATE customer SET name = ? , real_age = ? , height = ? WHERE id = ?" ; args.add(id); } return true; } /** * public List< customer > List () {String SQL = "SELECT id, name, real_age, height FROM customer"; return Dbs.getTpl().query(sql,new BeanPropertyRowMapper<>(Customer.class)); } /** * public customer find(Integer id) {String SQL = "SELECT id, name, real_age, height FROM customer WHERE id = ?" ; return Dbs.getTpl().queryForObject(sql, new BeanPropertyRowMapper<>(Customer.class), id); Public Boolean remove(Integer id) {String SQL = "DELETE FROM customer WHERE id =?" ; return Dbs.getTpl().update(sql,id) > 0; }}Copy the code

Create a unit test folder

Create a test folder in the same directory as SRC

Mark directory as — test Resources Root

  • Create the file CustomerDaoTest.
@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class CustomerDaoTest { private static CustomerDAO dao; @BeforeClass public static void beforeClass() { dao = new CustomerDAO(); System.out.println("beforeClass"); } @AfterClass public static void afterClass() { System.out.println("afterClass"); } @Before public void before() { System.out.println("before"); } @After public void after() { System.out.println("after"); } @Test public void test_01_save() { CustomerDAO d = new CustomerDAO(); Customer customer= new Customer(); Customer. Elegantly-named setName (" fruge "); customer.setRealAge(20); Customer. SetHeight (1.89); Assert.assertTrue(d.save(customer)); } @test public void test_02_find() {assert.assertNotnull ("find Test failed ",dao.find(10)); } @Test public void test_03_list() { List<Customer> customers = dao.list(); AssertTrue ("list test not passed ",customers. Size () > 0); } @Test public void test_04_remove() { Assert.assertTrue(dao.remove(10)); }}Copy the code

Order of execution: beforeClass — before — test_01_save — after — before — test_02_find — after — before — test_03_list — after — before — test_04_remove — after — afterClass

Resources are typically created in before and released after so that each method uses resources that do not interfere with each other.

The execution order of the methods to be tested

@fixmethodOrder (methodsorters.name_Ascending) controls the method execution order of the test.

  1. The DEFAULT order is determined by the method name hashcode value. If the hash value is the same, the dictionary order is determined by the name. Different execution sequences may occur. On a certain operating system, the sequence of multiple executions remains the same.

  2. Methodsorters.name_ascending (recommended) Sorts by method name. Specifying the order of execution in this manner is consistent because of the dictionary order of characters; However, this method requires certain naming rules for test methods, such as test methods are named with test_NN_fun (NN indicates test method serial number 01-99), such as test_01_bindByBadData and test_02_bindByCorrectData

  3. MethodSorters. The JVM executes in the order in which method names are returned by the JVM, and the order in which test methods are executed in this way is unpredictable, meaning that the order may be different from run to run (especially in JDK7).

perform

Right click. or



Execute the entire test class



Executing a single method

Quickly create

【Generate】–【Test…】