Junit测试是很方便的,本博客记录一下Junit测试一些Service接口的方法,这样可以不运行项目,在@Test注解的方法里直接测试
Maven引入jar包:
UTF-8
4.12
junit
junit
${junit.version}
test
方法一、IOC方法
通过ApplicationContext,ioc的方法来获取对应的bean
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
private ApplicationContext applicationContext;
@Before
public void setUp() throws Exception {
applicationContext = new ClassPathXmlApplicationContext(
"classpath:spring/spring-context.xml");
}
@Test
public void test1(){
UnitService unitService = (UnitService) applicationContext
.getBean