junit 测试springMVC的Controller、Service层代码

本文介绍如何使用junit对SpringMVC的Controller和服务层进行测试。通过注解方式和直接使用Java代码加载上下文进行测试。注解方式需确保引入正确版本的依赖,而加载上下文可以通过ClassPathXmlApplicationContext或FileSystemXmlApplicationContext,它们可以读取Spring应用上下文。同时提到了classpath和classpath*的区别,classpath仅查找class路径,而classpath*会包括jar文件中的class路径。

用junit对springMVC的Controller(Service同理)进行测试

注解的方式

java代码:
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-servlet.xml")
public class ControllerTestFirst {
    @Autowired
    private YourController yourController;

    @Test
    public void yourTest(){
        yourController.yourMethod();
    }
}

需要导入的包:
   以下是maven的pom.xml文件内容,若包的版本不正确,不互相匹配,可能执行会报错。以下引入的包作为参考。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hxj.TestCode</groupId>
    <artifactId>test_code</artifactId>
    <version>1.0-SNAPSHOT</version>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
       <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.1.4.RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>


直接用java代码的方式

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class ControllerTestSecond {
    @Test
    public void yourTest(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring-servlet.xml");
//        ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:spring-servlet.xml");
        YourController yourController = ac.getBean(YourController.class);
        yourController.yourMethod();
    }
}

ApplicationContext ac = new ClassPathXmlApplicationContext("spring-servlet.xml");
ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:spring-servlet.xml");

这两种方式都可以读取Spring应用上下文,读取的都是classpath(WEB-INF/classes)路径下的文件。


classpath 和 classpath* 区别:

classpath:只会到class路径中查找找文件;

classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找。
优先级: lib>classes



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值