Skip to content

Commit 87547f2

Browse files
committed
Add test for @SpyBean for generic bean definition
Add an integration test to check that @SpyBean can be used with on a bean definition that returns a generic type. Closes gh-7625
1 parent bd74c3d commit 87547f2

File tree

4 files changed

+136
-1
lines changed

4 files changed

+136
-1
lines changed

spring-boot-dependencies/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2664,4 +2664,4 @@
26642664
<id>integration-test</id>
26652665
</profile>
26662666
</profiles>
2667-
</project>
2667+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2012-2016 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+
* http://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.boot.test.mock.mockito;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.boot.test.mock.mockito.example.ExampleGenericService;
24+
import org.springframework.boot.test.mock.mockito.example.ExampleGenericServiceCaller;
25+
import org.springframework.boot.test.mock.mockito.example.SimpleExampleIntegerGenericService;
26+
import org.springframework.boot.test.mock.mockito.example.SimpleExampleStringGenericService;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.context.annotation.Import;
30+
import org.springframework.test.context.junit4.SpringRunner;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.mockito.Mockito.verify;
34+
35+
/**
36+
* Test {@link SpyBean} on a test class field can be used to replace existing beans.
37+
*
38+
* @author Phillip Webb
39+
* @see SpyBeanOnTestFieldForExistingBeanCacheIntegrationTests
40+
*/
41+
@RunWith(SpringRunner.class)
42+
public class SpyBeanOnTestFieldForExistingGenericBeanIntegrationTests {
43+
44+
// gh-7625
45+
46+
@SpyBean
47+
private ExampleGenericService<String> exampleService;
48+
49+
@Autowired
50+
private ExampleGenericServiceCaller caller;
51+
52+
@Test
53+
public void testSpying() throws Exception {
54+
assertThat(this.caller.sayGreeting()).isEqualTo("I say 123 simple");
55+
verify(this.exampleService).greeting();
56+
}
57+
58+
@Configuration
59+
@Import({ ExampleGenericServiceCaller.class,
60+
SimpleExampleIntegerGenericService.class })
61+
static class SpyBeanOnTestFieldForExistingBeanConfig {
62+
63+
@Bean
64+
public ExampleGenericService<String> simpleExampleStringGenericService() {
65+
// In order to trigger issue we need a method signature that returns the
66+
// generic type not the actual implementation class
67+
return new SimpleExampleStringGenericService();
68+
}
69+
70+
}
71+
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2012-2016 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+
* http://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.boot.test.mock.mockito.example;
18+
19+
/**
20+
* Example generic service implementation for spy tests.
21+
*
22+
* @author Phillip Webb
23+
*/
24+
public class SimpleExampleIntegerGenericService
25+
implements ExampleGenericService<Integer> {
26+
27+
@Override
28+
public Integer greeting() {
29+
return 123;
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2012-2016 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+
* http://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.boot.test.mock.mockito.example;
18+
19+
/**
20+
* Example generic service implementation for spy tests.
21+
*
22+
* @author Phillip Webb
23+
*/
24+
public class SimpleExampleStringGenericService implements ExampleGenericService<String> {
25+
26+
@Override
27+
public String greeting() {
28+
return "simple";
29+
}
30+
31+
}

0 commit comments

Comments
 (0)