企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
``` 1. 添加依赖 添加spring-test 2. 通过@RunWith注解,指定spring的运行器 Spring的运行器是SpringJunit4ClassRunner 3. 通过@ContextConfiguration注解,指定spring运行器需要的配置文件路径 4. 通过@Autowired注解给测试类中的变量注入数据 ``` ### pom ``` <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.7.RELEASE</version> <scope>test</scope> </dependency> ``` ### 测试 ~~~ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class testUserService { @Autowired private UserService userService; @Test public void testIoc(){ userService.saveUser(); } } ~~~