I am using in my application Spring Security OAuth2. When I try to test the service layer with JUnit tests I get this error:
org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the 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(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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.