浏览代码

引入 spring Junit

zhangxiaoyu 2 年之前
父节点
当前提交
817e268c34
共有 3 个文件被更改,包括 36 次插入8 次删除
  1. 7 0
      pom.xml
  2. 8 8
      spring_annotation/src/main/resources/context.xml
  3. 21 0
      spring_annotation/src/test/java/SpringJunitTest.java

+ 7 - 0
pom.xml

@@ -48,6 +48,13 @@
48 48
             <artifactId>druid</artifactId>
49 49
             <version>1.1.13</version>
50 50
         </dependency>
51
+        <dependency>
52
+            <groupId>org.springframework</groupId>
53
+            <artifactId>spring-test</artifactId>
54
+            <version>5.2.2.RELEASE</version>
55
+
56
+            <!--            <version></version>-->
57
+        </dependency>
51 58
     </dependencies>
52 59
 
53 60
 </project>

+ 8 - 8
spring_annotation/src/main/resources/context.xml

@@ -11,13 +11,13 @@
11 11
 <!--&lt;!&ndash;        <constructor-arg name="userDao" ref="UserDao"></constructor-arg>&ndash;&gt;-->
12 12
 <!--    </bean>-->
13 13
 <!--    加载外部 properties文件-->
14
-    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>
15
-    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
16
-        <property name="driverClassName" value="${jdbc.driver}"></property>
17
-        <property name="url" value="${jdbc.url}"></property>
18
-        <property name="username" value="${jdbc.username}"></property>
19
-        <property name="password" value="${jdbc.password}"></property>
20
-    </bean>
14
+<!--    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>-->
15
+<!--    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">-->
16
+<!--        <property name="driverClassName" value="${jdbc.driver}"></property>-->
17
+<!--        <property name="url" value="${jdbc.url}"></property>-->
18
+<!--        <property name="username" value="${jdbc.username}"></property>-->
19
+<!--        <property name="password" value="${jdbc.password}"></property>-->
20
+<!--    </bean>-->
21 21
     <!--    组件扫描-->
22
-<!--    <context:component-scan base-package="com"></context:component-scan>-->
22
+    <context:component-scan base-package="com"></context:component-scan>
23 23
 </beans>

+ 21 - 0
spring_annotation/src/test/java/SpringJunitTest.java

@@ -0,0 +1,21 @@
1
+import com.Config.SpringConfiguration;
2
+import com.service.StudentService;
3
+import org.junit.Test;
4
+import org.junit.runner.RunWith;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.test.context.ContextConfiguration;
7
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
8
+// 让测试运⾏于Spring测试环 境,以便在测试开始的时候⾃动创建Spring的应⽤上下⽂
9
+@RunWith(SpringJUnit4ClassRunner.class)
10
+// 使用配置文件的形式
11
+//@ContextConfiguration("classpath:context.xml")
12
+// 使用配置类
13
+@ContextConfiguration(classes = {SpringConfiguration.class})
14
+public class SpringJunitTest {
15
+    @Autowired
16
+    private StudentService service;
17
+    @Test
18
+    public void test1() {
19
+        service.showStudent();
20
+    }
21
+}