springboot项目用junit进行单元测试,加载bean main方法进行测试
@RunWith(SpringRunner.class)
// SourceItemApplication 必须是你自己springboot启动类
@SpringBootTest(classes = SourceItemApplication.class)
@WebAppConfiguration
public class TestAdapter {
@Autowired
private BaseAdminUserServiceImpl a;
@Test
public void a() {
Map<String, Object> delAdminUser;
try {
delAdminUser = a.delAdminUser(1, 1);
System.out.println(delAdminUser);
} catch (Exception e) {
e.printStackTrace();
}
}
}
==========================main方法加载bean=====================================
1.ClassPathXmlApplicationContext 方法加载bean
public static void main(String[] args) {
//ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:testBean.xml");
ApplicationContext factory=new AnnotationConfigApplicationContext(TestBeanConfiguration.class);
TestBean bean = (TestBean)factory.getBean("testBean");
System.out.println(bean.getStr());
}
public class TestBean {
public String getStr(){
return "test";
}
}
@Configuration
@ImportResource(value = {"classpath:testBean.xml"})
public class TestBeanConfiguration {
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="testBean" class="com.example.source.item.entity.TestBean" >
</bean>
</beans>
2.ClassPathXmlApplicationContext 直接加载xml资源目录
public static void main(String[] args) {
ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:testBean.xml");
//ApplicationContext factory=new AnnotationConfigApplicationContext(TestBeanConfiguration.class);
TestBean bean = (TestBean)factory.getBean("testBean");
System.out.println(bean.getStr());
}