I hava this code ,
@RunWith(SpringJUnit4ClassRunner.class)
public class JunitDemo {
@Test
public void testAssertArrayEquals() {
byte[] expected = "trial".getBytes();
byte[] actual = "trial".getBytes();
Assert.assertArrayEquals("fail", expected, actual);
}
}
and run the test, there are errors
Caused by: java.lang.IllegalArgumentException: Can not load an ApplicationContext with a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration. at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:276) at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304) ... 28 more
then, i find a same Q with SO,the solution is
@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);
}
}
in fact,for this pojo, i do'nt need the xml config. and i will get other error
Caused by: java.io.FileNotFoundException: class path resource [/JunitDemo-context.xml] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328) ... 37 more
How to correctly run my program ?
Add something like this
where the xml contains test context (in simplest case it's the same as application context) to load/autowire all dependencies