mockit.integration.springframework.FakeBeanFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmockit Show documentation
Show all versions of jmockit Show documentation
JMockit is a Java toolkit for automated developer testing.
It contains APIs for the creation of the objects to be tested, for mocking dependencies, and for faking external
APIs; JUnit (4 & 5) and TestNG test runners are supported.
It also contains an advanced code coverage tool.
/*
* Copyright (c) 2006 Rogério Liesenfeld
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit.integration.springframework;
import javax.annotation.*;
import mockit.*;
import mockit.internal.injection.*;
import mockit.internal.state.*;
import org.springframework.beans.factory.support.*;
/**
* If applied, this fake will take over calls to {@link AbstractBeanFactory#getBean(String)} and
* {@link AbstractBeanFactory#getBean(String, Class)} in any implementation class, returning instead a
* {@link Tested @Tested} or {@link Injectable @Injectable} object with the given field name, or a dependency object
* injected at any level into a @Tested object.
*
* In case said calls come (indirectly) from a test class having no @Tested fields, bean lookup will proceed
* into the actual getBean implementation method.
*
* Note this fake is only useful if the code under test makes direct calls to Spring's getBean methods.
*/
public final class FakeBeanFactory extends MockUp
{
@Mock
public Object getBean(@Nonnull Invocation invocation, @Nonnull String name)
{
TestedClassInstantiations testedClasses = TestRun.getTestedClassInstantiations();
if (testedClasses == null) {
return invocation.proceed();
}
BeanExporter beanExporter = testedClasses.getBeanExporter();
Object bean = BeanLookup.getBean(beanExporter, name);
return bean;
}
@Mock
public T getBean(@Nonnull Invocation invocation, @Nonnull String name, @Nullable Class requiredType)
{
TestedClassInstantiations testedClasses = TestRun.getTestedClassInstantiations();
if (testedClasses == null) {
return invocation.proceed();
}
BeanExporter beanExporter = testedClasses.getBeanExporter();
T bean = BeanLookup.getBean(beanExporter, name, requiredType);
return bean;
}
@Mock
public T getBean(@Nonnull Invocation invocation, @Nonnull Class requiredType)
{
TestedClassInstantiations testedClasses = TestRun.getTestedClassInstantiations();
if (testedClasses == null) {
return invocation.proceed();
}
BeanExporter beanExporter = testedClasses.getBeanExporter();
T bean = BeanLookup.getBean(beanExporter, requiredType);
return bean;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy