entity
package tech.pdai.springboot2unit5.entity; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import javax.persistence.*; /** * User. */ @Entity @Getter @Setter @NoArgsConstructor @AllArgsConstructor @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column private String name; @Column private String phone; }dao
package tech.pdai.springboot2unit5.dao; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import tech.pdai.springboot2unit5.entity.User; /** * user dao. */ @Repository public interface UserRepository extends JpaRepository<User, Integer> { }service
package tech.pdai.springboot2unit5.service; import tech.pdai.springboot2unit5.entity.User; import java.util.List; /** * user service. */ public interface IUserService { /** * find all user. * * @return list */ List<User> findAll(); } package tech.pdai.springboot2unit5.service.impl; import org.springframework.stereotype.Service; import tech.pdai.springboot2unit5.dao.UserRepository; import tech.pdai.springboot2unit5.entity.User; import tech.pdai.springboot2unit5.service.IUserService; import java.util.List; /** * User service impl. */ @Service public class UserServiceImpl implements IUserService { /** * user dao. */ private final UserRepository userRepository; /** * init. * * @param userRepository2 user dao */ public UserServiceImpl(final UserRepository userRepository2) { this.userRepository = userRepository2; } /** * find all user. * * @return list */ @Override public List<User> findAll() { return userRepository.findAll(); } }Controller
package tech.pdai.springboot2unit5.controller; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import tech.pdai.springboot2unit5.entity.User; import tech.pdai.springboot2unit5.service.IUserService; import java.util.List; /** * User controller. */ @RestController public class UserController { /** * user service. */ private final IUserService userService; /** * init. * * @param userService2 user service */ public UserController(final IUserService userService2) { this.userService = userService2; } /** * find user list. * * @return list */ @GetMapping("user/list") public ResponseEntity<List<User>> list() { return ResponseEntity.ok(userService.findAll()); } } 测试类在实际的项目中可以使用profile来区分测试ut,使用test profile(包含H2内存库),真实环境使用MySQL或其它。
main
package tech.pdai.springboot2unit5; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Profile; import org.springframework.test.context.junit.jupiter.SpringExtension; import tech.pdai.springboot2unit5.service.IUserService; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; /** * A way to test from H2. * <p> * Just a demo, and change profile to 'test' for H2, and 'product' for MySQL. */ @Profile("default") @ExtendWith(SpringExtension.class) @SpringBootTest class JavaSpringbootUnit5ApplicationTests { @Autowired IUserService userService; @Test @DisplayName("Integration test") void contextLoads() { assertFalse(userService.findAll().isEmpty()); assertEquals("pdai", userService.findAll().get(0).getName()); } }controller
mockMvc
package tech.pdai.springboot2unit5.controller; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import tech.pdai.springboot2unit5.entity.User; import tech.pdai.springboot2unit5.service.IUserService; import java.util.Collections; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; /** * user controller test - use mockito. */ @ExtendWith(SpringExtension.class) @WebMvcTest(value = UserController.class) class UserControllerTest { @Autowired private MockMvc mockMvc; @MockBean private IUserService userService; /** * test find all user. * * @throws Exception exception */ @Test @DisplayName("Test findAll()") public void list() throws Exception { Mockito.when(userService.findAll()).thenReturn( Collections.singletonList(new User(1, "pdai.tech", "1221111"))); mockMvc.perform(MockMvcRequestBuilders.get("/user/list") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()); verify(userService, times(1)).findAll(); } }