spring整合junit中使用到的依赖以及简单的测试案例演示

2023-09-07

注意:spring整合junit中的依赖要和spring-context的依赖版本相一致

复制代码
 <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.11.RELEASE</version>
</dependency>
复制代码

测试

注意注意:

@RunWith里面的内容是
SpringJUnit4ClassRunner.class
声明的是spring和junit整合之后的运行类,不能只是junit类
复制代码
package com.hh.service;

import com.hh.config.SpringConfig;
import com.hh.domain.Account;
import org.junit.Test;
import org.junit.internal.runners.JUnit4ClassRunner;
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;

import java.util.List;

/**
 * @author hh
 * @version 1.0
 * @DATE 2023-09-07 15:04:15
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class AccountServiceTest {
    
    @Autowired
    private AccountService accountService;
    
    @Test
    public void testAccountService(){
        List<Account> accounts = accountService.selectAll();
        System.out.println("accounts = " + accounts);
    }
}
复制代码

 

posted @   努力是一种常态  阅读(47)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
历史上的今天:
2022-09-07 MySQL第五天
点击右上角即可分享
微信分享提示