2.spring-boot>测试>junit

pom.xml



    4.0.0

    org.example
    untitled
    1.0-SNAPSHOT

    
    
        spring-boot-starter-parent
        org.springframework.boot
        2.6.4
    

    
        8
        8
    

    
        
        
            org.springframework.boot
            spring-boot-starter
        
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
            junit
            junit
            test
        
    


主启动Application.class

package com.spring.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

Service/UserService.class

package com.spring.boot.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {
    public void add(){
        System.out.println("调用add······");
    }
}

TestApplication.class

package com.spring.boot;

import com.spring.boot.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(classes = Application.class) // spring-boot测试类
@RunWith(SpringRunner.class)  // 整合junit框架
public class TestApplication {
    @Autowired
     UserService userService;

    @Test
    public void addTest(){
        userService.add();
    }
}

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