私はSpring Boot 1.4.2を使用しています。私はSpring Bootで新しくなっています。ここで SpringBootTest模擬認証ユーザがカスタムユーザで動作しない
public static String getCurrentUserToken(){
return ((AuthenticatedUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();
}
は私のカスタムAuthenticatedUserさ: 私は、コントローラのためのアドバイスで におけるユーザログが、私は以下のように現在のuserIdを取得するための呼び出しを行ったときに、現在のユーザー情報を設定するための認証フィルタを持っています:
public class AuthenticatedUser implements Serializable {
private final String userName;
private final String userId;
private final String sessionId;
public AuthenticatedUser(String userName, String userId, String sessionId) {
super();
this.userName = userName;
this.userId = userId;
this.sessionId = sessionId;
}
public String getUserName() {
return userName;
}
public String getUserId() {
return userId;
}
public String getSessionId() {
return sessionId;
}
}
すべてが正常に動作します。 しかし、フィルタは統合テストでは機能しません。私は現在のユーザーをモックする必要があります。 私はユーザーを模倣する方法をたくさん探しましたが、それらのどれも私を助けませんでした。私は最終的に私が望んでいたものに近い可能性があり、このガイドを発見:以下https://aggarwalarpit.wordpress.com/2017/05/17/mocking-spring-security-context-for-unit-testing/ はそのガイドライン以下の私のテストクラスである:私は数日を過ごした
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
AuthenticatedUser cannot be returned by getAuthentication()
getAuthentication() should return Authentication
:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
public class PersonalLoanPreApprovalTest {
@Before
public void initDB() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void testRequestPersonalLoanPreApproval_Me() {
AuthenticatedUser applicationUser = new
AuthenticatedUser("[email protected]", "2d1b5ae3", "123");
UsernamePasswordAuthenticationToken authentication = new ApiKeyAuthentication(applicationUser);
SecurityContext securityContext = mock(SecurityContext.class);
when(securityContext.getAuthentication()).thenReturn(authentication);
SecurityContextHolder.setContext(securityContext);
// error at this line
when(securityContext.getAuthentication().getPrincipal()) .thenReturn(applicationUser);
// The controller for this api has the advice to get the userId
MyResponse response = restTemplate.getForObject(url.toString(), MyResponse.class);
}
}
私はこのエラーを得ましたこれは既に私が見つけた多くの提案を試みましたが、まだ失敗しました。私もエラーの原因となる行を削除しようとしましたが、エラーがなくなっても、私のコントローラのアドバイスで現在のユーザー情報を取得できませんでした。
どのような提案も高く評価されています。
UPDATE1:私の結果にコードを少し修正したかっただけです。私は今、テストクラス内のエラーを取り除くことができます
@Test
public void testRequestPersonalLoanPreApproval_Me() {
AuthenticatedUser applicationUser = new AuthenticatedUser("[email protected]", "2d1b5ae3-cf04-44f5-9493-f0518cab4554", "123");
Authentication authentication = Mockito.mock(Authentication.class);
SecurityContext securityContext = Mockito.mock(SecurityContext.class);
Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
SecurityContextHolder.setContext(securityContext);
Mockito.when(authentication.getPrincipal()).thenReturn(applicationUser);
// The controller for this api has the advice to get the userId
MyResponse response = restTemplate.getForObject(url.toString(), MyResponse.class);
}
}
:@glitchの提案で
は、私は以下のように私の試験方法で認証し、ユーザーを欺くためにコードを変更しました。コードにデバッグしました。私がテストクラスに入っているときにsecurityContextに値があることを確認してください。私は、コントローラのアドバイス内のコードにジャンプするときには、以下の戻りをnullを取得:
SecurityContextHolder.getContext().getAuthentication().getPrincipal()
try: 'when(securityContext.getAuthentication())thenReturn((認証)認証); ' – mangotang
あなたの提案に感謝します。私はそれを試して、まだ行に古いエラーがあります: '(securityContext.getAuthentication()。getPrincipal())。then return(applicationUser);'。私のために他の提案がありますか? – Sal