我正在为我的@Configuration类编写测试,并在自动装配ApplicationContext时崩溃。包中只有一个Config类。奇怪的是,bean在正常执行中工作。
这是测试的基础。
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AWSConfig.class)
@TestPropertySource(properties = {""})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@PowerMockIgnore({"javax.management.*", "sun.*", "javax.net.ssl.*"})
@PrepareForTest({AWSConfig.class})
@ComponentScan(basePackages = {"com.mycompany..."})
public class AppConfigTest {
@Autowired
private ApplicationContext context;
由于无法加载上下文而失败,根本原因是找不到类。
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
Caused by: java.lang.ClassNotFoundException: com.mycompany.AWSConfig$$EnhancerBySpringCGLIB$$e9f0fdf
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:178)
at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:68)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:250)
at org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:284)
... 46 more
答案 0 :(得分:0)
使用@ComponentScan(basePackages = {"com.mycompany.*"})
代替@ComponentScan(basePackages = {"com.mycompany..."})
或
只需使用@ComponentScan("com.mycompany.*")