What is the difference between SpringRunner and SpringJUnit4ClassRunner?
@RunWith() @RunWith()
What is the difference between these two?
SpringRunner inherits SpringJUnit4ClassRunner and does not extend any functions; the former is just a short name.
SpringRunner cannot map to SpringJUnit4ClassRunner
If you can see in the Maven dependency that the library has been loaded, you must remove <scope>test</scope>
<dependency> <groupId></groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
<dependency> <groupId></groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency>
PS: scope classification
compile (compile scope)
- The default is compile, and nothing is configured means compile.
- compile means that the dependent project needs to participate in the compilation of the current project. When the test is continued, the running cycle will also participate in it, which is a relatively strong dependency.
- It usually needs to be included when packing.
test (test range)
- scope means that the dependent project only participates in testing related work, including the compilation and execution of test code.
- A typical example is junit.
runtime (runtime range)
- runntime means that the dependent project does not need to participate in the project's compilation, but it requires its participation in later testing and run cycles. Compared with compile, just skipping compilation. To be honest, in terminal projects (non-open source, internal enterprise systems), there is not much difference between compile and compile.
- A more common implementation such as JSR×××, the corresponding API jar is compile, and the specific implementation is runtime. Compile only needs to know the interface.
- Oracle jdbc driver rack package is a good example, generally scope is runntime.
- In addition, the dependency of runntime is usually used with optional, and optional is true. I can implement it with A or B.
provided (Scope provided)
- Provided means that you can not need to pack it in when packing, and other facilities (Web Container) will provide it.
- In fact, this dependency can theoretically participate in compilation, testing, running and other cycles.
- It is equivalent to compile, but the exclude action is done during the packaging stage.
- For example, if you develop a web application, you may need the available Servlet API in the compilation classpath to compile a servlet, but you won't want to include this Servlet API in a packaged WAR; this Servlet API JAR is provided by your application server or servlet container.
- The scoped dependencies are available when compiling classpath (not runtime).
- They are not transitive and will not be packaged.
system (system scope)
- System scope dependencies are similar to provided, but you must explicitly provide a path to the JAR file in the local system.
- This is done to allow compilation based on local objects that are part of the system class library. Such a component should always be available, and Maven will not look for it in the warehouse.
- If you set a dependency scope to system scope, you must also provide a systemPath element.
- Note that this scope is not recommended (you should always try to reference dependencies from public or customized Maven repositories).
Dependency delivery of scope
A–>B–>C. The current project is A, A depends on B, and B depends on C. If you know the scope of B in project A, then how do you know the scope of C in project A?
The answer is:
- When C is test or provided, C is discarded directly, and A does not depend on C;
- Otherwise, A depends on C, and the scope of C inherits from B's scope.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.