soundsystem例子
创建CD接口
package com.zachary.soundsystem;
/**
* @author Zachary.Zheng
* @version 1.0
* @date 2020年6月20日 下午9:20:07
*/
public interface CompactDisc {
void play();
}
创建SgtPeppers类实现CompactDisc 接口,使用@Component注解
@Component
public class SgtPeppers implements CompactDisc {
private String title = "Sgt. Pepper's Lonely Hearts Club Band";
private String artist = "The Beatles";
@Override
public void play() {
System.out.println("Playing " + title + " by " + artist);
}
}
创建CDPlayerConfig类,使用@ComponentScan
默认主键扫描是不启动的,需要显示的配置一下Spring,命令它寻找带有@Component注解的类,并为其创建Bean。
@Configuration
@ComponentScan
public class CDPlayerConfig {
}
Junit测试类创建
快速创建测试类
右键需要创建的测试类,选择New->Other->JUnit ->JUnit Test Case
择sourceFolder,为测试类命名,点击Next
选择需要测试的方法,点击Finish就完成了。
测试类CDPlayerConfigTest,使用@WunWith和@ContextConfiguration注解
使用SpringJUnit4ClassRunner,以便在测试的时候自动创建Spring上下文。
ContextConfiguration设置加载的配置。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig.class)
public class CDPlayerConfigTest {
@Autowired
private CompactDisc cd;
@Test
public void cdShouldNotBeNullTest() {
assertNotNull(cd);
}
}
测试结果
创建测试类遇到坑总结
报错信息
java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=cdShouldNotBeNullTest], {ExactMatcher:fDisplayName=cdShouldNotBeNullTest(com.zachary.soundsystem.CDPlayerConfigTest)], {LeadingIdentifierMatcher:fClassName=com.zachary.soundsystem.CDPlayerConfigTest,fLeadingIdentifier=cdShouldNotBeNullTest]] from org.junit.internal.requests.ClassRequest@21b8d17c
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:40)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createFilteredTest(JUnit4TestLoader.java:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:74)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:49)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:525)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
引起报错原因
- Spring版本和JUnit版本不兼容
- 需要hamcrest-all-1.3.jar和commons-logging-1.2.jar两个jar包
- 代码中需要用到的包只有spring-beans-5.2.7.RELEASE.jar,spring-context-5.2.7.RELEASE.jar,spring-test-5.2.7.RELEASE.jar包。只引入这三个包,虽然代码不报错,但是spring创建上下文时还用到了spring-core-5.2.7.RELEASE.jar,spring-aop-5.2.7.RELEASE.jar,spring-aspects-5.2.7.RELEASE.jar,spring-expression-5.2.7.RELEASE.jar包。如果没有引入测试的时候也会报错。
最终确定最少能执行所需的jar包以及版本如下
相关jar包下载地址
https://repo.spring.io/libs-release-local/org/springframework/spring/5.2.7.RELEASE/
http://commons.apache.org/proper/commons-logging/download_logging.cgi
https://mvnrepository.com/artifact/commons-logging/commons-logging/1.2
https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all/1.3
https://mvnrepository.com/artifact/junit/junit/4.12
转载:https://blog.csdn.net/ZacharyZheng1/article/details/106882899