@RunWith(SpringRunner.class)
@RunWithについてご紹介します!
@RunWithは、次のコードで構成されています.
@Reentionアレイのメモリを取得するタイミングを設定します.
内部設定の保存ポリシーはenumクラスで、次のようになります.
@Targetは、貼り付け可能なロケータのターゲットを指定します.
内部設定値ElementTypeは、次のようにenumクラスです.
@Inheritedでは、宣言をサブクラスに継承できます.
このメソッドのRunnerクラスを返し、単一のクラスを実行するコンストラクション関数が必要です.
ここでSpringRunnerclassはSpringJUnit 4 ClassRunnerと同じ意味で、JUnitクラスまたはJUnitクラスの親が@RunWithに置き換えられた場合、default RunnerではなくTest Runnerを使用してJUnit Frameworkが呼び出されます.
したがって、@RunWith操作により、使用するRunnerをSpringJUnit 4 ClassRunnerとして指定できます.
参照[https://4whomtbts.tistory.com/128]を参照してください.
JUnitのバージョンを5に変更し、JUnit 5から@RunWithではなく@ExtendWithを使用してテストする方法をカスタマイズします.
@SpringBootTestも使用できます.@ExtendWithは@SpringBootTestクラスで直接@ExtendWithを使用する必要がないためです.
参照[https://www.whiteship.me/springboot-no-more-runwith/]を参照してください.
@RunWithは、次のコードで構成されています.
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
public @interface RunWith {
/**
* @return a Runner class (must have a constructor that takes a single Class to run)
*/
Class<? extends Runner> value();
}
コードを一つ一つ理解してみましょう.@Retention(RetentionPolicy.RUNTIME)
@Reentionアレイのメモリを取得するタイミングを設定します.
内部設定の保存ポリシーはenumクラスで、次のようになります.
public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
*/
SOURCE,
/**
* Annotations are to be recorded in the class file by the compiler
* but need not be retained by the VM at run time. This is the default
* behavior.
*/
CLASS,
/**
* Annotations are to be recorded in the class file by the compiler and
* retained by the VM at run time, so they may be read reflectively.
*
* @see java.lang.reflect.AnnotatedElement
*/
RUNTIME
}
したがって、「保存ポリシー」(RetentionPolicy.RUNTIME)では、実行時にアレイ内のメモリを使用できます.JVMはjavaバイトコードを含むクラスファイルでランタイム環境を構成し、ランタイムが終了するまでメモリが存在します.@Target(ElementType.TYPE)
@Targetは、貼り付け可能なロケータのターゲットを指定します.
内部設定値ElementTypeは、次のようにenumクラスです.
public enum ElementType {
/** Class, interface (including annotation type), or enum declaration */
TYPE,
/** Field declaration (includes enum constants) */
FIELD,
/** Method declaration */
METHOD,
/** Formal parameter declaration */
PARAMETER,
/** Constructor declaration */
CONSTRUCTOR,
/** Local variable declaration */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** Package declaration */
PACKAGE,
/**
* Type parameter declaration
*
* @since 1.8
*/
TYPE_PARAMETER,
/**
* Use of a type
*
* @since 1.8
*/
TYPE_USE,
/**
* Module declaration.
*
* @since 9
*/
MODULE
}
したがって、「ターゲット」(ElementType.TYPE)は、このアイテムを貼り付けるターゲットをクラス、インタフェース(コメントタイプを含む)または列挙に設定します.@Inherited
@Inheritedでは、宣言をサブクラスに継承できます.
public @interface RunWith
このメソッドのRunnerクラスを返し、単一のクラスを実行するコンストラクション関数が必要です.
@RunWith(SpringRunner.class)
ここでSpringRunnerclassはSpringJUnit 4 ClassRunnerと同じ意味で、JUnitクラスまたはJUnitクラスの親が@RunWithに置き換えられた場合、default RunnerではなくTest Runnerを使用してJUnit Frameworkが呼び出されます.
したがって、@RunWith操作により、使用するRunnerをSpringJUnit 4 ClassRunnerとして指定できます.
参照[https://4whomtbts.tistory.com/128]を参照してください.
なぜ最新バージョンのスプリングガイドを使用したアイテムのテストコードに@RunWithが表示されないのか
JUnitのバージョンを5に変更し、JUnit 5から@RunWithではなく@ExtendWithを使用してテストする方法をカスタマイズします.
@SpringBootTestも使用できます.@ExtendWithは@SpringBootTestクラスで直接@ExtendWithを使用する必要がないためです.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@BootstrapWith(SpringBootTestContextBootstrapper.class)
@ExtendWith(SpringExtension.class)
public @interface SpringBootTest {
...
参照[https://junit.org/junit5/docs/current/user-guide/]を参照してください.参照[https://www.whiteship.me/springboot-no-more-runwith/]を参照してください.
Reference
この問題について(@RunWith(SpringRunner.class)), 我々は、より多くの情報をここで見つけました https://velog.io/@jurlring/RunWithSpringRunner.classテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol