在JUnit5之前,我们用@RunWith(MockitoJUnitRunner.class)注解,使用Mockito来做单元测试。使用JUnit5我们用@ExtendWith(MockitoExtension.class)注解,来做单元测试。这时我们同时使用@Mock/@InjectMocks/@Spy注解来配合写单元测试用例。
目前,可以说项目中都会使用Spring。那测试用例中怎么引入Spring呢?在JUnit5之前,我们可以使用@RunWith(SpringJUnit4ClassRunner.class)注解。而使用JUnit5我们用@ExtendWith(SpringExtension.class)注解,同时使用@MockBean来配合写单元测试用例。
@ExtendWith(MockitoExtension.class)
import static org.assertj.core.api.BDDAssertions.then;
import static org.mockito.BDDMockito.given;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.core.userdetails.UserDetails;
import com.wanxun.recp.authorization.server.entity.TbPermission;
import com.wanxun.recp.authorization.server.entity.TbUser;
import com.wanxun.recp.authorization.server.service.ITbPermissionService;
import com.wanxun.recp.authorization.server.service.ITbUserService;
import com.wanxun.recp.authorization.server.service.impl.UserDetailsServiceImpl;
@ExtendWith(MockitoExtension.class)
public class UserDetailsServiceTest {
@Mock
private ITbUserService tbUserService;
@Mock
private ITbPermissionService tbPermissionService;
@InjectMocks
private UserDetailsServiceImpl userDetailsService;
@BeforeEach
public void setup() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
}
@Test
public void testLoadUser() {
//given
String username = "flynn";
TbUser user = new TbUser();
user.setCreated(LocalDateTime.now());
user.setEmail("flynn@sina.com");
user.setId(1L);
user.setPassword("123456");
user.setPhone("123456789");
user.setUsername(username);
List<TbPermission> tbPermissions = Arrays.asList(TbPermission.builder().created(user.getCreated()).id(user.getId()).name(username).build());
//given
given(tbUserService.getByUsername(username)).willReturn(user);
given(tbPermissionService.selectByUserId(user.getId())).willReturn(tbPermissions);
// BBDMockito.given().willReturn();
//when
UserDetails userDetails = userDetailsService.loadUserByUsername(username);
//then
then(userDetails.getUsername()).isEqualTo(user.getUsername());
}
}
@ExtendWith(SpringExtension.class)
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
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.autoconfigure.json.AutoConfigureJsonTesters;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import com.wanxun.recp.authorization.server.controller.TestController;
import com.wanxun.recp.authorization.server.entity.TbPermission;
import com.wanxun.recp.authorization.server.entity.TbUser;
import com.wanxun.recp.authorization.server.service.ITbPermissionService;
import com.wanxun.recp.authorization.server.service.ITbUserService;
import com.wanxun.recp.authorization.server.service.impl.UserDetailsServiceImpl;
@ExtendWith(SpringExtension.class)
@AutoConfigureJsonTesters
@WebMvcTest(TestController.class)
@TestPropertySource("classpath:/test.properties")
public class TestControllerTest {
@MockBean
private BCryptPasswordEncoder passwordEncoder;
@MockBean
private ITbUserService tbUserService;
@MockBean
private ITbPermissionService tbPermissionService;
@Autowired
private UserDetailsServiceImpl userDetailsService;
@Autowired
private MockMvc mvc;
@Test
public void testGetUserDetails() throws Exception {
String username = "flynn";
TbUser user = new TbUser();
user.setCreated(LocalDateTime.now());
user.setEmail("flynn@sina.com");
user.setId(1L);
user.setPassword("123456");
user.setPhone("13702443392");
user.setUsername(username);
List<TbPermission> tbPermissions = Arrays.asList(TbPermission.builder().created(user.getCreated()).id(user.getId()).name(username).build());
//given
given(tbUserService.getByUsername(username)).willReturn(user);
given(tbPermissionService.selectByUserId(user.getId())).willReturn(tbPermissions);
// MockHttpServletResponse response = mvc.perform(
// post("/test/userDetails").contentType(MediaType.APPLICATION_JSON)
// .content(jsonRequestAttempt.write(attemptDTO).getJson()))
// .andReturn().getResponse();
MockHttpServletResponse response = mvc.perform(
get("/test/userDetails").param("username", username)).andReturn().getResponse();
// .andReturn().getResponse();
System.out.println(response.getContentAsString());
}
}