[英]Spring Boot junit testing secured application
I am using in my application Spring Security OAuth2. 我在我的应用程序Spring Security OAuth2中使用。 When I try to test the service layer with JUnit tests I get this error:
当我尝试使用JUnit测试来测试服务层时,出现以下错误:
org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext
org.springframework.security.authentication.AuthenticationCredentialsNotFoundException:在SecurityContext中找不到身份验证对象
Can you tell me how to fix it? 你能告诉我如何解决吗?
Here is my Test class: 这是我的测试班:
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class FooServiceTest {
@Autowired
private FooService service;
@Test
public void someTest() {
service.findFoo(1L);
}
}
Use @WithMockUser
使用
@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.