私はSpringアプリケーションを持っており、Controller
をテストするためにJUnit
テストをビルドしています。Springセキュリティに依存するJUnitでSpringコントローラをテストする
問題がController
の内側に、私はこのコードを呼び出すことです。つまり
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
final String userName = authentication.getName();
を、私はこのController
を呼び出す前に認証する必要があります。
private MockMvc mockMvc;
@Test
public void getPageTest() throws Exception{
final ProcessFileController controller = new ProcessFileController();
mockMvc = standaloneSetup(controller).build();
mockMvc.perform(get(URI.create("/processFile.html")).sessionAttr("freeTrialEmailAddress", "")).andExpect(view().name("processFile"));
}
そして私は、私がログインしていないので、私のauthentication
がnull
あるので、それは私にfinal String userName = authentication.getName();
にNullPointerException
権利を与えて実行します。私はこのコードでJUnit
テストを書きました。
質問:認証を模擬する方法はありますか?すべてのアイデアは大歓迎です。
ありがとうございます。
は、なぜあなたは手動でそれを行う代わりにAuthenticationPrincipal' @ '使用していますか? – chrylis
chrylis、アーキテクチャは '@ AuthenticationPrincipal'を使うようには設計されていませんでしたが、私は現在のアーキテクチャでこれを処理する方法が必要です。あなたの答えをありがとう。 –