-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Duplicate annotation exception with MockMvc and method-level annotations #11436
Comments
Hi @theseion, thanks for reaching out. Before getting into the actual issue, I'd like to clarify if you are trying to do a unit or integration test? @WebMvcTest(ImportController.class)
class MyTestClass {
@MockBean private MyBeanService myBeanService;
private MockMvc mockMvc;
@BeforeEach
void setUp() {
mocks = MockitoAnnotations.openMocks(this);
mockMvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
}
@AfterEach
void tearDown() throws Exception {
mocks.close();
}
@Test
void uploadFile() throws Exception {
var file = new MockMultipartFile(/*...*/);
mockMvc
.perform(
multipart("/import")
.file(file)
.with(oidcLogin())
.with(csrf())
.with(user("user").roles("EXPERT")))
.andExpect(status().isOk());
/* verification etc. */
// verify(myBeanService).method(...);
}
} Using |
Thanks for getting back to me @marcusdacoregio. It's really an integration test and I need to pull up the entire context. I've updated the issue with a link to an MWE. |
I'm having the same issue during the migration to spring boot 3.1.1, findUniqueAnnotation is throwing an exception saying it finds twice the @PreAuthorize annotation. After debugging I see twice the same proxy with the same method, it should not happen |
I believe that this is the same problem as #13490 |
Yes, it seems so. In my case is PreAuthorize but the case is the same |
Describe the bug
While upgrading from Spring Boot 2.5.6 to 2.7.1 I noticed an issue with
MockMvc
and method-level@PreAuthorize
annotations. The issue appears to be that the anonymous class that Mockito creates includes the same annotations on the methods as the original class. The new checks for duplicate annotations (introduced in 5.6) now see the same annotation twice, once on the original method and once on the anonymous override.To Reproduce
This is my test setup (only relevant parts included):
Running this test produces
AnnotationConfigurationException
inAuthorizationAnnotationUtils#findUniqueAnnotation
due to a duplicate@PreAuthorize
annotation on theuploadFile()
method:Expected behavior
Identical annotations on overridden methods should be possible and not throw an exception.
Sample
MWE: https://github.com/theseion/spring-security-mwe
Versions (Spring Boot 5.7.2):
Spring Security: 5.7.2
Spring Boot Test: 2.7.1
Spring Test 5.3.21
The text was updated successfully, but these errors were encountered: