Spring 整合 Junit测试

1. 引入依赖坐标

        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.13.2version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>5.3.20version>
        dependency>

2. 测试类

package com.xin.web.service;

import com.xin.config.SpringConfig;
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 = SpringConfig.class) //Spring配置类
class UserServiceTest {

    @Autowired
    private UserService userService;

    @Test
    public void testFindAll() {
        userService.findAll();
    }
}

你可能感兴趣的:(Spring,junit,spring,java)