Issue
I wrote test class:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@WebAppConfiguration
@Transactional
public class ProfileTest {
@Autowired
private UserService userService;
@Autowired
private ProfileService profileService;
@Autowired
private InterestService interestService;
private SiteUser[] users = {
new SiteUser("[email protected]", "testPass"),
new SiteUser("[email protected]", "testPass"),
new SiteUser("[email protected]", "testPass"),
};
private String[][] interests = {
{"music", "guitar_xxxxxx", "plants"},
{"music", "music", "philosophy_19"},
{"random", "football"},
};
@Test
public void testInterests() {
for(int i=0; i<users.length; i++) {
SiteUser user = users[i];
String[] interestArray = interests[i];
userService.register(user);
HashSet<Interest> interestSet = new HashSet<>();
for(String interestText : interestArray) {
Interest interest = interestService.createIfNotExists(interestText);
interestSet.add(interest);
assertNotNull("interest should not be null", interest);
assertNotNull("Interest should have ID", interest.getId());
assertEquals("Text should match", interestText, interest.getName());
}
Profile profile = new Profile(user);
profile.setInterests(interestSet);
profileService.save(profile); // ERROR 1
Profile retrievedProfile = profileService.getUserProfile(user); // ERROR 2
assertEquals("Interest sets should match", interestSet, retrievedProfile.getInterests());
}
}
}
But this error happens on lines that have comments ERROR 1 and ERROR 2 and I found out why is that happening however I don't know how to fix it or if there's any workaround.
Problem is in here:
@Service
public class ProfileService {
@Autowired
ProfileDao profileDao;
@PreAuthorize("isAuthenticated()") // ERROR 1
public void save(Profile profile) {
profileDao.save(profile);
}
@PreAuthorize("isAuthenticated()") // ERROR 2
public Profile getUserProfile(SiteUser user) {
return profileDao.findByUser(user);
}
}
These '@PreAuthorize("isAuthenticated()")' annotations are causing the problem, because when I try to use those two methods in my UnitTest, I'm getting this exception: (AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext):
org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.credentialsNotFound(AbstractSecurityInterceptor.java:379)
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:223)
at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:65)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
at com.socialnetwork.service.ProfileService$$EnhancerBySpringCGLIB$$22c09751.save(<generated>)
at com.socialnetwork.tests.ProfileTest.testInterests(ProfileTest.java:72)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
And if I remove these annotations, my test passes. So do you know how can I fix my tests?
Solution
Why do you want to skip those annotations? did you tried @WithMockUser to have a mocked user as part of your context?
Here you have how to use that annotation.
Answered By - geeksusma