[英]Spring Boot junit testing secured application
我在我的應用程序Spring Security OAuth2中使用。 當我嘗試使用JUnit測試來測試服務層時,出現以下錯誤:
org.springframework.security.authentication.AuthenticationCredentialsNotFoundException:在SecurityContext中找不到身份驗證對象
你能告訴我如何解決嗎?
這是我的測試班:
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class FooServiceTest {
@Autowired
private FooService service;
@Test
public void someTest() {
service.findFoo(1L);
}
}
使用@WithMockUser
@WithMockUser(username = "usertest", password = "password", roles = "USER")
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class FooServiceTest {
@Autowired
private FooService service;
@Test
public void someTest() {
service.findFoo(1L);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.