@RunWith(SpringRunner.class)@SpringBootTestpublicclassApplicationTests{privatestaticfinalLoggerlog=LoggerFactory.getLogger(ApplicationTests.class);@ResourceprivateCustomerServiceservice;/**
* 测试增删改查
*/@Testpublicvoidtest(){service.deleteAll();// save a couple of customersservice.save(newCustomer("Alice","Smith"));service.save(newCustomer("Bob","Smith"));// fetch all customersSystem.out.println("Customers found with findAll():");System.out.println("-------------------------------");intcount=0;for(Customercustomer:service.findAll()){System.out.println(customer);count++;}assertThat(count,is(2));// fetch an individual customerSystem.out.println("Customer found with findByFirstName('Alice'):");System.out.println("--------------------------------");Customerc=service.findByFirstName("Alice");assertThat(c,notNullValue());assertThat(c.getFirstName(),is("Alice"));System.out.println("Customers found with findByLastName('Smith'):");System.out.println("--------------------------------");List<Customer>list=service.findByLastName("Smith");assertThat(list,notNullValue());assertThat(list.size(),greaterThan(1));assertThat(list.size(),is(2));}}
在上面的测试中,我先删掉数据库中所有客户数据,然后新增两条数据,查询所有的数据,根据姓或名查询。
运行测试结果为green bar,同时输出:
1
2
3
4
5
6
7
Customers found with findAll():
-------------------------------
Customer[id=5a9a30e616afd63f48afeea6, firstName='Alice', lastName='Smith']
Customer[id=5a9a30e616afd63f48afeea7, firstName='Bob', lastName='Smith']
Customer found with findByFirstName('Alice'):
--------------------------------
Customers found with findByLastName('Smith'):