Spring整合Junit
在pom.xml添加依赖
<dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.12version> <scope>testscope> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-testartifactId> <version>5.3.14version> dependency>
然后在Java测试类中使用注解
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({"classpath:spring-config.xml"}) public class SpringTest { ? @Resource private Master master; //依赖注入 ? @Test public void testMaster(){ master.sayHello(); } ? ? // @Test // public void test() { // ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml"); // //Master master = new Master(); // Master master = (Master) applicationContext.getBean("master"); // master.sayHello(); // } }