引用
1.Java单元测试技巧之PowerMock
集成依赖
<dependencyManagement>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
控制层代码
@Slf4j
@RestController
public class LoginController {
@Autowired
private LoginService loginService;
@PostMapping("/login")
public BaseResponse<String> login(@RequestBody LoginUserEntity loginUser) {
String token = loginService.loginSys(loginUser);
log.info("用户 {} 登录成功", loginUser.getUsername());
return BaseResponse.success(token);
}
@GetMapping("/test/page")
public BaseResponse<String> testPage() {
return BaseResponse.success("test page");
}
}
光标对准要测试的方法,快捷键command+shift+t会在test目录下创建对应的测试目录
测试代码
RunWith(PowerMockRunner.class)
public class LoginControllerTest {
@Mock
private LoginService loginService;
@InjectMocks
private LoginController loginController;
@Test
public void loginSuccessTest() {
String token = "this is token";
LoginUserEntity loginUserEntity = LoginUserEntity.builder()
.username("admin")
.password("admin")
.build();
Mockito.when(loginService.loginSys(loginUserEntity)).thenReturn(token);
BaseResponse<String> baseResponse = loginController.login(loginUserEntity);
Assert.assertEquals(200, baseResponse.getCode());
Assert.assertEquals("success", baseResponse.getMsg());
Assert.assertEquals(token, baseResponse.getData());
}
@Test
public void testPage() {
BaseResponse<String> baseResponse = loginController.testPage();
Assert.assertEquals(200, baseResponse.getCode());
Assert.assertEquals("success", baseResponse.getMsg());
}
}
service代码
@Service
@Slf4j
public class LoginServiceImpl implements LoginService {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private JwtTokenUtil jwtTokenUtil;
@Override
public String loginSys(LoginUserEntity loginUser) {
final Authentication authentication = authenticate(loginUser.getUsername(), loginUser.getPassword());
//存储认证信息
SecurityContextHolder.getContext().setAuthentication(authentication);
//生成token
log.info("{} 登录,生成token", loginUser.getUsername());
final SysUserDetails userDetail = (SysUserDetails) authentication.getPrincipal();
final String token = jwtTokenUtil.generateAccessToken(userDetail);
//存储token
jwtTokenUtil.putToken(loginUser.getUsername(), token);
return token;
}
private Authentication authenticate(String username, String password) {
try {
// 该方法会去调用userDetailsService.loadUserByUsername()去验证用户名和密码,
// 如果正确,则存储该用户名密码到security 的 context中
return authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
} catch (DisabledException | BadCredentialsException e) {
throw new PermissionDeniedException("用户名或密码错误,请重新登录");
}
}
}
?测试代码
@RunWith(PowerMockRunner.class)
public class LoginServiceImplTest {
@InjectMocks
private LoginServiceImpl loginService;
@Mock
private AuthenticationManager authenticationManager;
@Mock
private JwtTokenUtil jwtTokenUtil;
@Test
public void loginSysSuccessTest() throws Exception {
String token = "this is token";
LoginUserEntity loginUserEntity = LoginUserEntity.builder()
.username("admin")
.password("admin")
.build();
SysUserDetails sysUserDetails = new SysUserDetails();
sysUserDetails.setId(1);
sysUserDetails.setUsername("admin");
sysUserDetails.setPassword("admin");
sysUserDetails.setRoles(null);
Mockito.when(authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(loginUserEntity.getUsername(), loginUserEntity.getPassword())))
.thenReturn(new UsernamePasswordAuthenticationToken(sysUserDetails, "admin"));
PowerMockito.when(loginService, "authenticate", loginUserEntity.getUsername(), loginUserEntity.getPassword())
.thenReturn(new UsernamePasswordAuthenticationToken(sysUserDetails, "admin"));
Mockito.when(jwtTokenUtil.generateAccessToken(sysUserDetails)).thenReturn(token);
String actualToken = loginService.loginSys(loginUserEntity);
Assert.assertEquals(token, actualToken);
}
@Test(expected = PermissionDeniedException.class)
public void loginSysFailedTest() throws Exception {
String token = "this is token";
LoginUserEntity loginUserEntity = LoginUserEntity.builder()
.username("admin")
.password("admin")
.build();
SysUserDetails sysUserDetails = new SysUserDetails();
sysUserDetails.setId(1);
sysUserDetails.setUsername("admin");
sysUserDetails.setPassword("admin");
sysUserDetails.setRoles(null);
Mockito.when(authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(loginUserEntity.getUsername(), loginUserEntity.getPassword())))
.thenThrow(new BadCredentialsException("bad credential"));
loginService.loginSys(loginUserEntity);
}
}
?测试结果
?
?
|