Spring 整合 Junit

一、添加 Maven 依賴

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.1.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.1.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

二、創建實體類

public class IOCBean {
	...
}

三、創建 Spring 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="icoBean" class="chu.yi.bo.IOCBean"/>
</beans>

四、創建測試類

@RunWith(SpringJUnit4ClassRunner.class)
// locations 屬性用於指定配置文件
// classes 屬性用於指定配置類
@ContextConfiguration(locations = {"classpath:Application.xml"})
public class TestSpring {
    @Autowired
    private IOCBean iocBean;

    @Test
    public void testJunit() {
        iocBean.sayHello();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章