• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / jmockit1 / 496

15 Nov 2025 05:33PM UTC coverage: 72.192% (-0.008%) from 72.2%
496

push

github

web-flow
Merge pull request #412 from hazendaz/renovate/major-spring-core

Update spring core to v7 (major)

5677 of 8360 branches covered (67.91%)

Branch coverage included in aggregate %.

11922 of 16018 relevant lines covered (74.43%)

0.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.73
/main/src/main/java/mockit/integration/springframework/FakeBeanFactory.java
1
/*
2
 * MIT License
3
 * Copyright (c) 2006-2025 JMockit developers
4
 * See LICENSE file for full license text.
5
 */
6
package mockit.integration.springframework;
7

8
import edu.umd.cs.findbugs.annotations.NonNull;
9
import edu.umd.cs.findbugs.annotations.Nullable;
10

11
import mockit.Injectable;
12
import mockit.Invocation;
13
import mockit.Mock;
14
import mockit.MockUp;
15
import mockit.Tested;
16
import mockit.internal.injection.BeanExporter;
17
import mockit.internal.injection.TestedClassInstantiations;
18
import mockit.internal.state.TestRun;
19

20
import org.springframework.beans.factory.support.AbstractBeanFactory;
21
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
22

23
/**
24
 * If applied, this fake will take over calls to {@link AbstractBeanFactory#getBean(String)} and
25
 * {@link AbstractBeanFactory#getBean(String, Class)} in any implementation class, returning instead a
26
 * {@link Tested @Tested} or {@link Injectable @Injectable} object with the given field name, or a dependency object
27
 * injected at any level into a <code>@Tested</code> object.
28
 * <p>
29
 * In case said calls come (indirectly) from a test class having no <code>@Tested</code> fields, bean lookup will
30
 * proceed into the actual <code>getBean</code> implementation method.
31
 * <p>
32
 * Note this fake is only useful if the code under test makes direct calls to Spring's <code>getBean</code> methods.
33
 */
34
public final class FakeBeanFactory extends MockUp<DefaultListableBeanFactory> {
1✔
35
    @Mock
36
    public static Object getBean(@NonNull Invocation invocation, @NonNull String name) {
37
        TestedClassInstantiations testedClasses = TestRun.getTestedClassInstantiations();
1✔
38

39
        if (testedClasses == null) {
1!
40
            return invocation.proceed();
×
41
        }
42

43
        BeanExporter beanExporter = testedClasses.getBeanExporter();
1✔
44
        return BeanLookup.getBean(beanExporter, name);
1✔
45
    }
46

47
    @Mock
48
    public static <T> T getBean(@NonNull Invocation invocation, @NonNull String name, @Nullable Class<T> requiredType) {
49
        TestedClassInstantiations testedClasses = TestRun.getTestedClassInstantiations();
1✔
50

51
        if (testedClasses == null) {
1!
52
            return invocation.proceed();
×
53
        }
54

55
        BeanExporter beanExporter = testedClasses.getBeanExporter();
1✔
56
        return BeanLookup.getBean(beanExporter, name, requiredType);
1✔
57
    }
58

59
    @Mock
60
    public static <T> T getBean(@NonNull Invocation invocation, @NonNull Class<T> requiredType) {
61
        TestedClassInstantiations testedClasses = TestRun.getTestedClassInstantiations();
1✔
62

63
        if (testedClasses == null) {
1!
64
            return invocation.proceed();
×
65
        }
66

67
        BeanExporter beanExporter = testedClasses.getBeanExporter();
1✔
68
        return BeanLookup.getBean(beanExporter, requiredType);
1✔
69
    }
70
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc