Spring整合Junit

整合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();
//    }
}

 

 
posted @ 2022-05-12 11:52  Resign~as  阅读(25)  评论(0)    收藏  举报