美文网首页
Spring学习笔记(6)- 注解方式测试

Spring学习笔记(6)- 注解方式测试

作者: 成长的烦恼c | 来源:发表于2019-03-11 19:45 被阅读0次
1.效果
2.引入jar包

注解方式用到了junit,导入所需jar包,方法见学习(4)
junit-4.12.jar和hamcrest-all-1.3.jar

3.testSpring.java

修改TestSpring, 并运行

  1. @RunWith(SpringJUnit4ClassRunner.class)
    表示这是一个Spring的测试类

  2. @ContextConfiguration("classpath:spring-config.xml")
    定位Spring的配置文件

  3. @Autowired
    给这个测试类装配Category对象

  4. @Test
    测试逻辑,打印c对象的名称

package com.jd.test;

import com.jd.java.Category;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-spring.xml")
public class testSpring {
    @Autowired
    Category c;

    @Test
    public void test(){
        System.out.println(c.getName());
    }
}

相关文章

网友评论

      本文标题:Spring学习笔记(6)- 注解方式测试

      本文链接:https://www.haomeiwen.com/subject/qqbbpqtx.html