PDF
Controller Test1使TDDSpringBootContentsIntegration Test ................................................................................. 1Controller Test ........................................................................................ 1Service ................................................................................................. 3Repository ............................................................................................. 3使使Integration Test@RunWith(SpringRunner.class)@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)public class IntegrationTest { @Autowired private TestRestTemplate testClient; @Test public void should_get_computer_list_when_call_list_computer_api() { ResponseEntity<List<ComputerDto>> response = testClient.exchange( "/computers", HttpMethod.GET, null, new ParameterizedTypeReference<List<ComputerDto>>() { }); assertEquals(HttpStatus.OK, response.getStatusCode()); }}使使Controller Test@RunWith(SpringRunner.class)@WebMvcTest(controllers = ComputerController.class)public class ComputerControllerTest { @Autowired Controller Test2 private MockMvc mockMvc; @Test public void should_get_a_list_when_get_computers() throws Exception { mockMvc.perform(MockMvcRequestBuilders.get("/computers")) .andExpect(status().isOk()); }}@RestController("/computers")public class ComputerController { @GetMapping public List<ComputerDto> getComputers() { return null; }} @MockBean private ComputerService computerService; @Test public void should_get_a_list_when_get_computers() throws Exception { given(computerService.getComputers()) .willReturn( Collections.singletonList( new ComputerDto(1, "MacBook 2015", "Haifeng Li", "2019-09-10") )); mockMvc.perform(MockMvcRequestBuilders.get("/computers")) .andExpect(status().isOk()) .andExpect(jsonPath("$", hasSize(1))) .andExpect(jsonPath("$[0].id").value(1)) .andExpect(jsonPath("$[0].type").value("MacBook 2015")) .andExpect(jsonPath("$[0].owner").value("Haifeng Li")) .andExpect(jsonPath("$[0].createTime").value("2019-09-10")) .andDo(print()); } Repository3Service@RunWith(MockitoJUnitRunner.class)public class ComputerServiceTest { @Mock private ComputerRepository computerRepository; @InjectMocks private ComputerService computerService; @Test public void should_return_computer_list_when_get_all_computers() throws ParseException { ComputerEntity stored = new ComputerEntity(1, "MacBook 2015", "Haifeng Li", new SimpleDateFormat("dd/MM/yyyy").parse("01/09/2019")); given(computerRepository.findAll()).willReturn(Collections.singletonList(stored)); List<ComputerDto> computers = computerService.getComputers(); assertEquals(1, computers.size()); assertEquals(1, computers.get(0).getId()); assertEquals("MacBook 2015", computers.get(0).getType()); assertEquals("Haifeng Li", computers.get(0).getOwner()); assertEquals("2019-09-01", computers.get(0).getCreateTime()); }}Repository使@RunWith(SpringRunner.class)@DataJpaTestpublic class ComputerRepositoryTest { @Autowired private TestEntityManager entityManager; @Autowired private ComputerRepository computerRepository; Repository4 @Before public void prepareData() throws ParseException { entityManager.persistAndFlush(new ComputerEntity(1, "MacBook 2015", "Haifeng Li", new SimpleDateFormat("dd/MM/yyyy").parse("01/09/2019")) ); entityManager.persistAndFlush(new ComputerEntity(2, "Desktop", null, new SimpleDateFormat("dd/MM/yyyy").parse("02/09/2019")) ); } @Test public void should_return_all_records_in_db_when_find_all() { List<ComputerEntity> entities = computerRepository.findAll(); assertEquals(2, entities.size()); }}

HTML view coming soon.

Download PDF for the full formatted version.