- pom.xml
<!-- Spring test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.3.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- junit的版本号需要大于等于 4.12 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
- test 类
@RunWith(SpringJUnit4ClassRunner.class) //使用 junit4 测试
@ContextConfiguration(classes = {Config.class}) //这里可以使用配置文件 xml, 也可以支持配置类
public class MyTest02 {
@Autowired //直接通过spring注解可以直接获得需要的类对象
private AccountService accountService;
/**
*test01 是使用传统方法实现, 作对比用的
*/
@Test
public void test01(){
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
AccountService bean = context.getBean(AccountService.class);
List<AccountBean> all = bean.findAll();
all.forEach(accountBean -> System.out.println(accountBean));
}
/**
* 直接使用spring 工厂类对象
*/
@Test
public void test02(){
List<AccountBean> all = accountService.findAll();
all.forEach(accountBean -> System.out.println(accountBean));
}
}
网友评论