Skip to content

Commit 3842f12

Browse files
committed
Verify that the MockitoExtension and SpringExtension can be combined
See gh-33742
1 parent 5f14703 commit 3842f12

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.bean.override.mockito.integration;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.extension.ExtendWith;
21+
import org.mockito.ArgumentCaptor;
22+
import org.mockito.Captor;
23+
import org.mockito.junit.jupiter.MockitoExtension;
24+
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
28+
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
29+
import org.springframework.test.context.junit.jupiter.SpringExtension;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.mockito.Mockito.verify;
33+
34+
/**
35+
* Integration tests that verify support for {@link MockitoBean @MockitoBean}
36+
* and {@link MockitoSpyBean @MockitoSpyBean} when the {@link MockitoExtension}
37+
* is registered alongside the {@link SpringExtension}.
38+
*
39+
* <p>This test class currently verifies explicit support for {@link Captor @Captor},
40+
* but we may extend the scope of this test class in the future.
41+
*
42+
* @author Sam Brannen
43+
* @since 6.2
44+
*/
45+
@ExtendWith(MockitoExtension.class)
46+
@ExtendWith(SpringExtension.class)
47+
public class SpringExtensionAndMockitoExtensionIntegrationTests {
48+
49+
@MockitoSpyBean
50+
RegistrationService registrationService;
51+
52+
@MockitoBean
53+
UserService userService;
54+
55+
@Captor
56+
ArgumentCaptor<User> userCaptor;
57+
58+
59+
@Test
60+
void test() {
61+
registrationService.registerUser("Duke");
62+
verify(registrationService).registerUser("Duke");
63+
verify(userService).validateUser(userCaptor.capture());
64+
assertThat(userCaptor.getValue().name).isEqualTo("Duke");
65+
}
66+
67+
@Configuration
68+
static class Config {
69+
70+
@Bean
71+
RegistrationService registrationService(UserService userService) {
72+
return new RegistrationService(userService);
73+
}
74+
}
75+
76+
interface UserService {
77+
78+
void validateUser(User user);
79+
}
80+
81+
record RegistrationService(UserService userService) {
82+
83+
void registerUser(String name) {
84+
User user = new User(name);
85+
this.userService.validateUser(user);
86+
// Register user...
87+
}
88+
}
89+
90+
record User(String name) {
91+
}
92+
93+
}

0 commit comments

Comments
 (0)