我有这个代码,
@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) 中不存在。 springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328) ... 37 更多
如何正确运行我的程序?</p>