新建Junit Test Case文件,单元测试有时要加载配置文件,注意两个注解
@RunWith(SpringRunner.class)
@SpringBootTest
public class XXX{
@Autowired
private XXX xxx;
@Test
public void test() {
xxx.方法();
}
}
如果普通SpringMVC使用applicationContxt.xml配置文件,可以这样获取bean注入,
//注解改成
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
这里的classpath就是resource