在之前的一篇文章中https://blog.csdn.net/qq_42013035/article/details/104002302我们讲了第一个简单Spring框架的应用配置
但在里面使用的测试方法是新建一个主类,然后在main方法里进行测试,但在显示开发中往往不这么做,因为请求服务端的方式是多样的,我们选择在 test/java中新建一个Spring测试类AppTest,接下来一步步来实现测试类的配置
本例的gitee地址:https://gitee.com/swingforyou/spring_framework_exercise.git
1.先在pom.xml 中导入测试类依赖的两个包
2.新建一个配置类AppConfig
package soundsystem;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//将扫描的注解转移过来
@ComponentScan
//表明这是一个配置类
@Configuration
public class AppConfig {
}
3.在 test/java下新建一个Spring测试单元AppTest
package soundsystem;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//暂时固定写法
@RunWith(SpringJUnit4ClassRunner.class)
//classes的值为配置类
@ContextConfiguration(classes = AppConfig.class)
public class AppTest {
//该注解表示该方法为测试方法
@Test
public void test(){
//测试代码
}
}