Spring整合Junit
在pom.xml添加依赖
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- 集成Junit --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.14</version> </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(); // } }
浙公网安备 33010602011771号