【发布时间】:2017-07-05 05:22:11
【问题描述】:
我有这个代码,
@RunWith(SpringJUnit4ClassRunner.class)
public class JunitDemo {
@Test
public void testAssertArrayEquals() {
byte[] expected = "trial".getBytes();
byte[] actual = "trial".getBytes();
Assert.assertArrayEquals("fail", expected, actual);
}
}
运行测试,有错误
原因:java.lang.IllegalArgumentException:无法加载 带有 NULL 'contextLoader' 的 ApplicationContext。考虑注释 使用@ContextConfiguration 的测试类。 在 org.springframework.util.Assert.notNull(Assert.java:112) 在 org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:276) 在 org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304) ... 28 更多
然后,我发现与 SO 相同的 Q,解决方案是
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class JunitDemo {
@Resource
private ApplicationContext ApplicationContext;
@Test
public void testAssertArrayEquals() {
byte[] expected = "trial".getBytes();
byte[] actual = "trial".getBytes();
Assert.assertArrayEquals("fail", expected, actual);
}
}
其实,对于这个pojo,我不需要xml配置。 我会得到其他错误
原因:java.io.FileNotFoundException:类路径资源 [/JunitDemo-context.xml] 无法打开,因为它不存在 在 org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158) 在 org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328) ... 37 更多
如何正确运行我的程序?
【问题讨论】:
-
您的应用中有
@Configuration类吗?或者你使用基于 xml 的配置?
标签: spring junit springjunit4classrunner