spring整合Junit纯注解-入门

7 篇文章 0 订阅
2 篇文章 0 订阅

 

一、环境

1. idea2018.1

2.maven 3.3.3

3. jdk1.8

二、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.ygq</groupId>
    <artifactId>spring_03_ annotation</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!--spring核心库-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <!--spring整合Junit-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
</project>

三、项目结构

四、实现步骤

1、spring核心配置文件相当于bean.xml

package com.ygq.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration //声明为spring配置文件
@ComponentScan("com.ygq") //开启包扫描
public class SpringConfiguration {
}

2、StudentSerivice.java

package com.ygq.service;

public interface StudentSerivice {
    String sayHello();

}

3、StudentSeriviceImpl.java

package com.ygq.service.impl;

import com.ygq.service.StudentSerivice;
import org.springframework.stereotype.Service;

@Service //bean
public class StudentServiceImpl implements StudentSerivice {
    public String sayHello() {

        System.out.println("I'm StudentServiceImpl!");
        return null;
    }
}

4、StudentController.Java

package com.ygq.controller;

import com.ygq.service.StudentSerivice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller //bean
public class StudentController {
    @Autowired
    private StudentSerivice studentSerivice;
    public  String sayHello(){
        studentSerivice.sayHello();
        return "I'm StudentController!";
    }
}

5、测试类

package com.ygq.controller;


import com.ygq.config.SpringConfiguration;
import com.ygq.service.StudentSerivice;
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(classes = SpringConfiguration.class) //关键在这里,换成了配置类
public class testStudentController {

    @Autowired
    private StudentController studentController;

    @Test
    public void testSayHello() {
        System.out.println(studentController.sayHello());
    }

}

结果

I'm StudentServiceImpl!
I'm StudentController!
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring JUnit5 注解测试是一种基于 JUnit5 平台的单元测试方法,通过使用 Spring 扩展提供的注解来实现依赖注入、AOP、事务控制等功能,从而方便地进行单元测试。 下面是 Spring JUnit5 注解测试常用的注解及其作用: 1. @ExtendWith(SpringExtension.class):指定运行测试的类为 SpringExtension,该类会创建 Spring 的上下文环境,并自动加载指定的配置文件或配置类。 2. @ContextConfiguration:指定 Spring 的配置文件或配置类,用于创建 Spring 的上下文环境。 3. @Autowired:自动装配 Spring 容器中的 Bean,可以省略 setter 方法。 4. @Transactional:在测试方法中添加该注解可以实现事务控制,测试方法执行完成后自动回滚事务。 5. @Test:用于标记测试方法。 下面是一个简单的 Spring JUnit5 注解测试的例子: ```java @ExtendWith(SpringExtension.class) @ContextConfiguration(classes = AppConfig.class) @Transactional public class UserServiceTest { @Autowired private UserService userService; @Test public void testAddUser() { User user = new User(); user.setUsername("test"); user.setPassword("123456"); userService.addUser(user); User addedUser = userService.getUserByName("test"); Assertions.assertEquals("test", addedUser.getUsername()); } } ``` 在该例子中,@ExtendWith 指定了运行测试的类为 SpringExtension,@ContextConfiguration 指定了需要加载的配置类 AppConfig,@Transactional 用于进行事务控制,@Autowired 实现了依赖注入,@Test 标记了测试方法。相比于 Spring JUnit4 注解测试,Spring JUnit5 注解测试更加简洁、易读,而且支持更多的测试特性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

再写一行代码就下班

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值