1. Spring 对 Junit4,Junit5 的支持上的运用
@
目录
每博一文案
- 关于理想主义,在知乎上看到一句话:“他们并不是不懂别人口中的现实,他们只是不信,事情只能是现在这个样子。”
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
- <context:component-scan base-package="com.rainbowsea.spring6.bean"></context:component-scan>
- </beans> 错误的、肮脏的、混乱的、轻松自如的、自言尽力了,有人真的不懂么?为了利益和怠惰做出这样那样或好或坏的事情,什么动物都会。有人会开脱和找借口,说着现实就是如此啊,无力改变世界就改变自己啊。他们对现状摇摇头然后果断投入到漆黑的污水里去,义无反顾地。
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
- <context:component-scan base-package="com.rainbowsea.spring6.bean"></context:component-scan>
- </beans> 但理想主义者是不信的,他们先是在语言上画一个大问号,再用行动画一个大叹号。
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
- <context:component-scan base-package="com.rainbowsea.spring6.bean"></context:component-scan>
- </beans> 他先问你,怎么就非得这样不可?怎么就没了别的办法?然后再用做出来的事情告诉你,我告诉你就是还能有更干净、更温和、更有效的办法!
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
- <context:component-scan base-package="com.rainbowsea.spring6.bean"></context:component-scan>
- </beans> 他们自己也有不能一尘不染的时候。但他们看着满目漆黑也不信世界上就没有洁白、干净这回事。
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
- <context:component-scan base-package="com.rainbowsea.spring6.bean"></context:component-scan>
- </beans> 理想主义者就是不会相信生活和世界就是这样了。不管它是别人眼中的好,还是坏。
- “一定有更好的办法,也能够通过合理的方式,缓慢地改善世界。愿意为了自己的理想而做些什么,甚至也愿意去牺牲什么。对于自己的手段、目的与认知,始终心怀疑问。即使做出的一切都看不到所谓的成效。即使这改变在活着的时候甚至也看不到。知道世界比较容易被忍受的这部分,是很多人用了很多代价换来的。但不会停下对好的东西的向往,还有向光明的地方行走的脚步。”
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
- <context:component-scan base-package="com.rainbowsea.spring6.bean"></context:component-scan>
- </beans> 这才算理想主义者吧。往往是重视精神价值超过物质利益。理想主义者把对精神价值的追求看作人生的主要追求,这些精神价值包括:尊严、正义、爱、美等等。
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
- <context:component-scan base-package="com.rainbowsea.spring6.bean"></context:component-scan>
- </beans>而浪漫主义,诺瓦利斯说过:
- “在我看来,把普通的东西赋予崇高的意义,给平凡的东西披上神秘的外衣,使熟知的东西恢复未知的尊严,对有限的东西给予无限的外观,这就是浪漫化。反过来说,这就是变得崇高、未知、神秘和无限的过程,通过这种联系,这一浪漫化过程就会被对数化。它得到一个常用的表达:浪漫哲学。”
复制代码 2. Spring对Junit4 的支持
准备工作,先在pom.xml 文件当中,导入相关依赖的 jar 包信息,如下:
准备好,对于的 Bean 对象用于测试:留意:必要将其 Bean 对象纳入到 Spring IOC 容器当中进行管理上。
 设置相关的:spring.xml 的设置信息的内容,这里我们用的是注解时开发,必要加上一个组件扫描 。
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
- <context:component-scan base-package="com.rainbowsea.spring6.bean"></context:component-scan>
- </beans>
复制代码 单位测试:(一样寻常寻常的单位测试)如下:
上述:方式,代码重复率高,复用性较差。
Spring提供的方便主要是这几个注解:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")留意该:spring.xml 文件要一定要在类路径下才行 。
在单位测试类上使用这两个注解之后,在单位测试类中的属性上可以使用 @Autowired。比较方便。- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration("classpath:spring.xml") // 注意该:spring.xml 文件要一定要在类路径下才行
复制代码 优化如下:
使用@Resource 必要导入相关的 jar 包,如下:

同时留意:该Juit4的@ Test 的注解是在:org.junit.Test; 包下的
3. Spring对Junit5的支持
引入JUnit5的依赖,Spring对JUnit支持的依赖还是:spring-test,同时换成,junit5 ,还是在 pom.xml 文件当值设置相关依赖的 jar 如下:
还是使用上面的 User 的 bean 对象进行测试:

在JUnit5当中,可以使用Spring提供的以下两个注解,标注到单位测试类上,这样在类当中就可以使用@Autowired注解了。- @ExtendWith(SpringExtension.class)
- @ContextConfiguration("classpath:spring.xml") // 同样一定要是在类路径下
复制代码 @ContextConfiguration("classpath:spring.xml") // 同样一定要是在类路径下
同时留意:该Juit5 的@ Test 的注解是在:org.junit.jupiter.api.Test 包下的 ,选择的时候不要,弄错了。
单位测试:
4. 总结:
- Spring 对 Junit4 的依赖的 jar 包,同时
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration("classpath:spring.xml") // 注意该:spring.xml 文件要一定要在类路径下才行
复制代码
- 同时留意:该Juit4的@Test 的注解是在:org.junit.Test; 包下的
- Spring 对 Junit5 的依赖的 jar 包,同时
- @ExtendWith(SpringExtension.class)
- @ContextConfiguration("classpath:spring.xml") // 同样一定要是在类路径下
复制代码
- 无论是 Junit4 还是 Junit5 当中的 @ContextConfiguration 注解当中的 @ContextConfiguration("classpath:spring.xml") 都是:一定要是在类路径下才行的。
- 关于:Spring 对 Junit4,Junit5 的支持运用,大家灵活选择运用即可,并不是非要选择其中的哪个哪个啥的。满意业务即可。
5. 末了:
“在这个末了的篇章中,我要表达我对每一位读者的感激之情。你们的关注和回复是我创作的动力源泉,我从你们身上吸取了无尽的灵感与勇气。我会将你们的鼓励留在心底,继续在其他的领域奋斗。感谢你们,我们总会在某个时刻再次相遇。”
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |