Skip to content

Commit fc8bca7

Browse files
committed
4.x: MockBean fails with ArgumentMatcher helidon-io#9397
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
1 parent 8f9f04b commit fc8bca7

File tree

3 files changed

+106
-3
lines changed

3 files changed

+106
-3
lines changed

microprofile/testing/mocking/src/main/java/io/helidon/microprofile/testing/mocking/MockBeansCdiExtension.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,27 @@ private void processMockBeanParameters(List<? extends AnnotatedParameter<?>> par
6868
});
6969
}
7070

71-
void registerOtherBeans(@Observes AfterBeanDiscovery event, BeanManager beanManager) {
71+
void registerMocks(@Observes AfterBeanDiscovery event, BeanManager beanManager) {
7272
// Register all mocks
7373
mocks.entrySet().forEach(entry -> {
74+
Object eagerMock = Mockito.mock(entry.getKey(),
75+
Mockito.withSettings().defaultAnswer(entry.getValue().answer()));
7476
event.addBean()
7577
.addType(entry.getKey())
7678
.scope(ApplicationScoped.class)
7779
.alternative(true)
7880
.createWith(inst -> {
81+
Object mock;
7982
Set<Bean<?>> beans = beanManager.getBeans(MockSettings.class);
8083
if (!beans.isEmpty()) {
8184
Bean<?> bean = beans.iterator().next();
8285
MockSettings mockSettings = (MockSettings) beanManager.getReference(bean, MockSettings.class,
8386
beanManager.createCreationalContext(null));
84-
return Mockito.mock(entry.getKey(), mockSettings);
87+
mock = Mockito.mock(entry.getKey(), mockSettings);
8588
} else {
86-
return Mockito.mock(entry.getKey(), Mockito.withSettings().defaultAnswer(entry.getValue().answer()));
89+
mock = eagerMock;
8790
}
91+
return mock;
8892
})
8993
.priority(0);
9094
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2024 Oracle and/or its affiliates.
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 io.helidon.microprofile.tests.testing.junit5;
18+
19+
import static org.hamcrest.CoreMatchers.is;
20+
import static org.hamcrest.MatcherAssert.assertThat;
21+
import static org.mockito.ArgumentMatchers.anyString;
22+
23+
import io.helidon.microprofile.testing.junit5.AddBean;
24+
import io.helidon.microprofile.testing.junit5.HelidonTest;
25+
import io.helidon.microprofile.testing.mocking.MockBean;
26+
27+
import org.junit.jupiter.api.Test;
28+
import org.mockito.Mockito;
29+
30+
@HelidonTest
31+
@AddBean(TestIssue9397.Service.class)
32+
class TestIssue9397 {
33+
34+
@MockBean
35+
private Service service;
36+
37+
@Test
38+
void testArgumentMatcher() {
39+
Mockito.when(service.test(anyString())).thenReturn("Mocked");
40+
assertThat(service.test("something"), is("Mocked"));
41+
}
42+
43+
static class Service {
44+
45+
String test(String test) {
46+
return test;
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2024 Oracle and/or its affiliates.
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 io.helidon.microprofile.tests.testing.testng;
18+
19+
import static org.hamcrest.CoreMatchers.is;
20+
import static org.hamcrest.MatcherAssert.assertThat;
21+
import static org.mockito.ArgumentMatchers.anyString;
22+
23+
import jakarta.inject.Inject;
24+
25+
import io.helidon.microprofile.testing.mocking.MockBean;
26+
import io.helidon.microprofile.testing.testng.HelidonTest;
27+
28+
import org.mockito.Mockito;
29+
import org.testng.annotations.Test;
30+
31+
@HelidonTest
32+
class TestIssue9397 {
33+
34+
@MockBean
35+
@Inject
36+
private Service service;
37+
38+
@Test
39+
void testArgumentMatcher() {
40+
Mockito.when(service.test(anyString())).thenReturn("Mocked");
41+
assertThat(service.test("something"), is("Mocked"));
42+
}
43+
44+
static class Service {
45+
46+
String test(String test) {
47+
return test;
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)