|
|
In the JMockit toolkit, the Faking API provides support for the creation of fake implementations.
JMockit ツールキットでは、Faking API が偽の実装の作成をサポートします。
Typically, a fake targets a few methods and/or constructors in the class to be faked, while leaving most other methods and constructors
unmodified.
通常、フェイクは、偽装するクラス内のいくつかのメソッドやコンストラクターを対象とし、他のほとんどのメソッドとコンストラクターは変更しません。
Fake implementations can be particularly useful in tests which depend on external components or resources such as e-mail or web services
servers, complex libraries, etc.
偽の実装は、電子メールや Web サービス サーバー、複雑なライブラリなどの外部コンポーネントやリソースに依存するテストで特に役立ちます。
Often, the fakes will be applied from reusable testing infrastructure components, not directly from a test class.
多くの場合、フェイクはテスト クラスから直接適用されるのではなく、再利用可能なテスト インフラストラクチャ コンポーネントから適用されます。
The replacement of real implementations with fake ones is completely transparent to the code which uses those dependencies, and can be
switched on and off for the scope of a single test, all tests in a single test class, or for the entire test run.
実際の実装を偽の実装に置き換えることは、それらの依存関係を使用するコードに対して完全に透過的であり、単一のテストのスコープ、単一のテスト クラス内のすべてのテスト、またはテスト実行全体に対してオンとオフを切り替えることができます。
In the context of the Faking API, a fake method is any method in a fake class that gets annotated with
@Mock
.
Faking API のコンテキストでは、偽メソッドとは、@Mock で注釈が付けられた偽クラス内の任意のメソッドです。
A fake class is any class extending the mockit.MockUp<T>
generic base class, where T
is the type
to be faked.
偽装クラスとは、mockit.MockUp<T> ジェネリック基本クラスを拡張するクラスです。ここで、T は偽装する型です。
The example below shows several fake methods defined in a fake class for our example "real" class,
javax.security.auth.login.LoginContext.
以下の例は、サンプルの「実際の」クラス javax.security.auth.login.LoginContext の偽のクラスで定義されたいくつかの偽のメソッドを示しています。
public final class FakeLoginContext extends MockUp<LoginContext> {
@Mock
public void $init(String name, CallbackHandler callback) {
assertEquals("test", name);
assertNotNull(callback);
}
@Mock
public void login() {}
@Mock
public Subject getSubject() { return null; }
}
When a fake class is applied to a real class, the latter gets the implementation of those methods and constructors which have
corresponding fake methods temporarily replaced with the implementations of the matching fake methods, as defined in the fake class.
偽のクラスが実際のクラスに適用されると、実際のクラスでは、対応する偽のメソッドを持つメソッドとコンストラクターの実装が、偽のクラスで定義されている一致する偽のメソッドの実装に一時的に置き換えられます。
In other words, the real class becomes "faked" for the duration of the test which applied the fake class.
つまり、偽のクラスを適用したテストの期間中、実際のクラスは「偽造」されたクラスになります。
Its methods will respond accordingly whenever they receive invocations during test execution.
メソッドは、テスト実行中に呼び出しを受け取るたびにそれに応じて応答します。
At runtime, what really happens is that the execution of a faked method/constructor is intercepted and redirected to the corresponding
fake method, which then executes and returns (unless an exception/error is thrown) to the original caller, without this one noticing that
a different method was actually executed.
実行時に実際に起こることは、偽造されたメソッド/コンストラクターの実行がインターセプトされ、対応する偽造メソッドにリダイレクトされ、その後、そのメソッドが実行されて元の呼び出し元に返される (例外/エラーがスローされない限り) ことです。このとき、呼び出し元は別のメソッドが実際に実行されたことに気付きません。
Normally, the "caller" class is one being tested, while the faked class is a dependency.
通常、「呼び出し元」クラスはテスト対象のクラスであり、偽装クラスは依存関係です。
Each @Mock
method must have a corresponding "real method/constructor" with the same signature in
the targeted real class.
各 @Mock メソッドには、対象となる実際のクラス内に同じシグネチャを持つ対応する「実際のメソッド/コンストラクター」が必要です。
For a method, the signature consists of the method name and parameters; for a constructor, it's just the parameters,
with the fake method having the special name "$init
".
メソッドの場合、シグネチャはメソッド名とパラメータで構成されます。コンストラクターの場合、シグネチャはパラメータのみで、偽のメソッドには特別な名前「$init」が付けられます。
Finally, notice there is no need to have fake methods for all methods and constructors in a real class.
最後に、実際のクラスのすべてのメソッドとコンストラクターに偽のメソッドを用意する必要がないことに注意してください。
Any such method or constructor for which no corresponding fake method exists in the fake class will simply stay "as is", that is, it
won't be faked.
偽クラス内に対応する偽メソッドが存在しないメソッドまたはコンストラクタは、単に「そのまま」残ります。つまり、偽造されません。
A given fake class must be applied to a corresponding real class to have any effect.
効果を得るには、特定の偽のクラスを対応する実際のクラスに適用する必要があります。
This is usually done for a whole test class or test suite, but can also be done for an individual test.
これは通常、テスト クラス全体またはテスト スイートに対して実行されますが、個々のテストに対して実行することもできます。
Fakes can be applied from anywhere inside a test class: a @BeforeClass
method, a
@BeforeMethod
/ @Before
/ @BeforeEach
method (TestNG / JUnit 4 / JUnit 5), or from a @Test
method.
フェイクは、テスト クラス内のどこからでも適用できます: @BeforeClass メソッド、@BeforeMethod / @Before / @BeforeEach メソッド (TestNG / JUnit 4 / JUnit 5)、または @Test メソッドから適用できます。
Once a fake class is applied, all executions of the faked methods and constructors of the real class get automatically redirected to the
corresponding fake methods.
偽のクラスが適用されると、実際のクラスの偽のメソッドとコンストラクターのすべての実行は、対応する偽のメソッドに自動的にリダイレクトされます。
To apply the FakeLoginContext
fake class above, we simply instantiate it:
上記の FakeLoginContext 偽のクラスを適用するには、次のようにインスタンス化するだけです。
@Test
public void applyingAFakeClass() throws Exception {
new FakeLoginContext());
// Inside an application class which creates a suitable CallbackHandler:
適切な CallbackHandler を作成するアプリケーション クラス内:
new LoginContext("test", callbackHandler).login();
...
}
Since the fake class is applied inside a test method, the faking of LoginContext
by
FakeLoginContext
will be in effect only for that particular test.
偽のクラスはテスト メソッド内で適用されるため、FakeLoginContext による LoginContext の偽装は、その特定のテストに対してのみ有効になります。
When the constructor invocation that instantiates LoginContext
executes, the corresponding "$init
"
fake method in FakeLoginContext
will be executed.
LoginContext をインスタンス化するコンストラクター呼び出しが実行されると、FakeLoginContext 内の対応する "$init" 偽メソッドが実行されます。
Similarly, when the LoginContext#login
method is called, the corresponding fake method will be executed, which in this case
will do nothing since the method has no parameters and void
return type.
同様に、LoginContext#login メソッドが呼び出されると、対応する偽のメソッドが実行されますが、この場合はメソッドにパラメーターがなく、戻り値の型が void であるため、何も実行されません。
The fake class instance on which these invocations occur is the one created in the first part of the test.
これらの呼び出しが発生する偽のクラス インスタンスは、テストの最初の部分で作成されたものです。
So far, we have only faked public instance methods with public instance fake methods.
これまでは、パブリック インスタンス メソッドをパブリック インスタンス偽装メソッドで偽装しただけです。
Several other kinds of methods in a real class can also be faked: methods with protected
or "package-private" accessibility,
static
methods, final
methods, and native
methods.
実際のクラス内の他のいくつかの種類のメソッドも偽装できます。保護されたメソッドまたは「パッケージプライベート」のアクセシビリティを持つメソッド、静的メソッド、最終メソッド、ネイティブ メソッドなどです。
Even more, a static
method in the real class can be faked by an instance fake method, and vice-versa (an instance
real method with a static
fake).
さらに、実際のクラスの静的メソッドは、インスタンスの偽のメソッドによって偽装することができ、その逆も同様です (インスタンスの実際のメソッドと静的な偽のメソッド)。
Methods to be faked need to have an implementation, though not necessarily in bytecode (in the case of native
methods).
偽装するメソッドには実装が必要ですが、必ずしもバイトコードである必要はありません (ネイティブ メソッドの場合)。
Therefore, an abstract
method cannot be faked directly.
したがって、抽象メソッドを直接偽装することはできません。
Note that fake methods don't need to be public
.
偽のメソッドは公開する必要がないことに注意してください。
To demonstrate this feature, lets consider the following code under test.
この機能を実証するために、次のテスト対象のコードを検討してみましょう。
public interface Service { int doSomething(); }
final class ServiceImpl implements Service { public int doSomething() { return 1; } }
public final class TestedUnit {
private final Service service1 = new ServiceImpl();
private final Service service2 = new Service() { public int doSomething() { return 2; } };
public int businessOperation() {
return service1.doSomething() + service2.doSomething();
}
}
The method we want to test, businessOperation()
, uses classes that implement a separate interface,
Service
.
テストするメソッド businessOperation() は、別のインターフェース Service を実装するクラスを使用します。
One of these implementations is defined through an anonymous inner class, which is completely inaccessible (except for the use of
Reflection) from client code.
これらの実装の 1 つは匿名の内部クラスを通じて定義されており、クライアント コードからは完全にアクセスできません (リフレクションを使用する場合を除く)。
Given a base type (be it an interface
, an abstract
class, or any sort of base class), we can write a test which
only knows about the base type but where all implementing/extending implementation classes get faked.
基本型 (インターフェース、抽象クラス、またはあらゆる種類の基本クラス) が与えられれば、基本型のみを認識し、実装/拡張するすべての実装クラスを偽装するテストを作成できます。
To do so, we create a fake whose target type refers only to the known base type, and does so through a type variable.
これを行うには、ターゲット型が既知の基本型のみを参照する偽物を作成し、型変数を介してそれを実行します。
Not only will implementation classes already loaded by the JVM get faked, but also any additional classes that happen to get loaded by
the JVM during later test execution.
JVM によってすでにロードされている実装クラスが偽装されるだけでなく、後のテスト実行中に JVM によってロードされる追加のクラスも偽装されます。
This ability is demonstrated below.
この能力は以下に示されます。
@Test
public <T extends Service> void fakingImplementationClassesFromAGivenBaseType() {
new MockUp<T>() {
@Mock int doSomething() { return 7; }
};
int result = new TestedUnit().businessOperation();
assertEquals(14, result);
}
In the test above, all invocations to methods implementing Service#doSomething()
will be redirected to the fake method
implementation, regardless of the actual class implementing the interface method.
上記のテストでは、インターフェース メソッドを実装する実際のクラスに関係なく、Service#doSomething() を実装するメソッドへのすべての呼び出しが偽のメソッド実装にリダイレクトされます。
When a class performs some work in one or more static initialization blocks, we may need to stub it out so it doesn't interfere
with test execution.
クラスが 1 つ以上の静的初期化ブロックで何らかの作業を実行する場合、テストの実行を妨げないようにスタブ化する必要がある場合があります。
We can define a special fake method for that, as shown below.
以下に示すように、そのために特別な偽のメソッドを定義できます。
@Test
public void fakingStaticInitializers() {
new MockUp<ClassWithStaticInitializers>() {
@Mock
void $clinit() {
// Do nothing here (usually).
ここでは何もしません (通常)。
}
};
ClassWithStaticInitializers.doSomething();
}
Special care must be taken when the static initialization code of a class is faked.
クラスの静的初期化コードが偽造されている場合は特別な注意が必要です。
Note that this includes not only any "static
" blocks in the class, but also any assignments to static
fields
(excluding those resolved at compile time, which do not produce executable bytecode).
これには、クラス内のすべての「静的」ブロックだけでなく、静的フィールドへのすべての割り当ても含まれることに注意してください (実行可能なバイトコードを生成しないコンパイル時に解決される割り当ては除きます)。
Since the JVM only attempts to initialize a class once, restoring the static initialization code of a faked class will have no
effect.
JVM はクラスの初期化を 1 回だけ試行するため、偽造されたクラスの静的初期化コードを復元しても効果はありません。
So, if you fake away the static initialization of a class that hasn't been initialized by the JVM yet, the original class initialization
code will never be executed in the test run.
したがって、JVM によってまだ初期化されていないクラスの静的初期化を偽装すると、元のクラス初期化コードはテスト実行で実行されなくなります。
This will cause any static fields that are assigned with expressions computed at runtime to instead remain initialized with the
default values for their types.
これにより、実行時に計算された式で割り当てられた静的フィールドは、代わりにその型のデフォルト値で初期化されたままになります。
A fake method can optionally declare an extra parameter of type mockit.Invocation
, provided it is the
first parameter.
偽のメソッドでは、最初のパラメータである場合に限り、オプションで mockit.Invocation 型の追加パラメータを宣言できます。
For each actual invocation to the corresponding faked method/constructor, an Invocation
object will be
automatically passed in when the fake method is executed.
対応する偽のメソッド/コンストラクターへの実際の呼び出しごとに、偽のメソッドが実行されると、Invocation オブジェクトが自動的に渡されます。
This invocation context object provides several getters which can be used inside the fake method.
この呼び出しコンテキスト オブジェクトは、偽のメソッド内で使用できるいくつかのゲッターを提供します。
One is the getInvokedInstance()
method, which returns the faked instance on which the invocation occurred
(null
if the faked method is static
).
1 つは getInvokedInstance() メソッドで、呼び出しが発生した偽造インスタンスを返します (偽造メソッドが静的である場合は null)。
Other getters provide the number of invocations (including the current one) to the faked method/constructor, the invocation arguments
(if any), and the invoked member (a java.lang.reflect.Method
or
java.lang.reflect.Constructor
object, as appropriate).
その他のゲッターは、偽装されたメソッド/コンストラクターへの呼び出し回数 (現在の呼び出しを含む)、呼び出し引数 (存在する場合)、および呼び出されたメンバー (適切な java.lang.reflect.Method オブジェクトまたは java.lang.reflect.Constructor オブジェクト) を提供します。
Below we have an example test.
以下にテスト例を示します。
@Test
public void accessingTheFakedInstanceInFakeMethods() throws Exception {
new MockUp<LoginContext>() {
Subject testSubject;
@Mock
void $init(Invocation invocation, String name, Subject subject) {
assertNotNull(name);
assertNotNull(subject);
// Verifies this is the first invocation.
これが最初の呼び出しであることを確認します。
assertEquals(1, invocation.getInvocationCount());
}
@Mock
void login(Invocation invocation) {
// Gets the invoked instance.
呼び出されたインスタンスを取得します。
LoginContext loginContext = invocation.getInvokedInstance();
assertNull(loginContext.getSubject()); // null until subject is authenticated
testSubject = new Subject();
}
@Mock
void logout() { testSubject = null; }
@Mock
Subject getSubject() { return testSubject; }
};
LoginContext theFakedInstance = new LoginContext("test", new Subject());
theFakedInstance.login();
assertSame(testSubject, theFakedInstance.getSubject();
theFakedInstance.logout();
assertNull(theFakedInstance.getSubject();
}
Once a @Mock
method is executing, any additional calls to the corresponding faked method are also
redirected to the fake method, causing its implementation to be re-entered.
@Mock メソッドが実行されると、対応する偽のメソッドへの追加の呼び出しも偽のメソッドにリダイレクトされ、その実装が再入力されます。
If, however, we want to execute the real implementation of the faked method, we can call the proceed()
method on the
Invocation
object received as the first parameter to the fake method.
ただし、偽のメソッドの実際の実装を実行したい場合は、偽のメソッドの最初のパラメーターとして受け取った Invocation オブジェクトで proceed() メソッドを呼び出すことができます。
The example test below exercises a LoginContext
object created normally (without any faking in effect at
creation time), using an unspecified configuration
.
以下のサンプル テストでは、指定されていない構成を使用して、通常どおりに作成された (作成時に偽装なしで) LoginContext オブジェクトを実行します。
@Test
public void proceedIntoRealImplementationsOfFakedMethods() throws Exception {
// Create objects used by the code under test:
テスト対象のコードで使用されるオブジェクトを作成します。
LoginContext loginContext = new LoginContext("test", null, null, configuration);
// Apply fakes:
偽物を適用する:
ProceedingFakeLoginContext fakeInstance = new ProceedingFakeLoginContext();
// Exercise the code under test:
テスト対象のコードを実行します。
assertNull(loginContext.getSubject());
loginContext.login();
assertNotNull(loginContext.getSubject());
assertTrue(fakeInstance.loggedIn);
fakeInstance.ignoreLogout = true;
loginContext.logout(); // first entry: do nothing
最初のエントリー: 何もしない
assertTrue(fakeInstance.loggedIn);
fakeInstance.ignoreLogout = false;
loginContext.logout(); // second entry: execute real implementation
2番目のエントリ: 実際の実装を実行する
assertFalse(fakeInstance.loggedIn);
}
static final class ProceedingFakeLoginContext extends MockUp<LoginContext> {
boolean ignoreLogout;
boolean loggedIn;
@Mock
void login(Invocation inv) throws LoginException {
try {
inv.proceed(); // executes the real code of the faked method
偽造されたメソッドの実際のコードを実行する
loggedIn = true;
}
finally {
// This is here just to show that arbitrary actions can be taken inside the fake, before and/or after the real method gets executed.
これは、実際のメソッドが実行される前または実行された後に、偽物の内部で任意のアクションを実行できることを示すためだけのものです。
LoginContext lc = inv.getInvokedInstance();
System.out.println("Login attempted for " + lc.getSubject());
}
}
@Mock
void logout(Invocation inv) throws LoginException {
// We can choose to proceed into the real implementation or not.
実際の実装に進むかどうかを選択できます。
if (!ignoreLogout) {
inv.proceed();
loggedIn = false;
}
}
}
In the example above, all the code inside the tested LoginContext
class will get executed, even though
some methods (login
and logout
) are faked.
上記の例では、一部のメソッド (ログインとログアウト) が偽装されているにもかかわらず、テスト対象の LoginContext クラス内のすべてのコードが実行されます。
This example is contrived; in practice, the ability to proceed into real implementations would not normally be useful for
testing per se, not directly at least.
この例は不自然なものです。実際には、実際の実装に進む能力は、少なくとも直接的には、テストそのものには通常役立ちません。
You may have noticed that use of Invocation#proceed(...)
in a fake method effectively behaves like advice (from AOP
jargon) for the corresponding real method.
偽のメソッドで Invocation#proceed(...) を使用すると、対応する実際のメソッドに対するアドバイス (AOP の専門用語) のように効果的に動作することに気付いたかもしれません。
This is a powerful ability that can be useful for certain things (think of an interceptor or decorator).
これは、特定の用途に役立つ強力な機能です (インターセプターやデコレーターなど)。
Often, a fake class needs to be used throughout multiple tests, or even applied for the test run as a whole.
多くの場合、偽のクラスは複数のテスト全体で使用したり、テスト実行全体に適用したりする必要があります。
One option is to use test setup methods that run before each test method; with JUnit, we use the
@Before
annotation; with TestNG, it's @BeforeMethod
.
1 つのオプションは、各テスト メソッドの前に実行されるテスト セットアップ メソッドを使用することです。JUnit では @Before アノテーションを使用し、TestNG では @BeforeMethod を使用します。
Another is to apply fakes inside of a test class setup method: @BeforeClass
.
もう 1 つは、テスト クラスのセットアップ メソッド @BeforeClass 内でフェイクを適用することです。
Either way, the fake class is applied by simply instantiating it inside the setup method.
どちらの場合でも、偽のクラスは、セットアップ メソッド内でインスタンス化するだけで適用されます。
Once applied, a fake will remain in effect for the execution of all tests in the test class.
一度適用されると、フェイクはテスト クラス内のすべてのテストの実行に対して有効なままになります。
The scope of a fake applied in a "before" method includes the code in any "after" methods the test class may have (annotated with
@After
for JUnit or @AfterMethod
for TestNG).
「before」メソッドに適用されたフェイクのスコープには、テスト クラスが持つ可能性のある「after」メソッドのコード (JUnit の場合は @After、TestNG の場合は @AfterMethod で注釈が付けられている) が含まれます。
The same goes for any fakes applied in a @BeforeClass
method: they will still be in effect during the
execution of any AfterClass
methods.
@BeforeClass メソッドに適用されたフェイクについても同様です。これらは AfterClass メソッドの実行中も有効です。
Once the last "after" or "after class" method finish being executed, though, all fakes get automatically "torn down".
ただし、最後の「after」または「after class」メソッドの実行が終了すると、すべてのフェイクは自動的に「破棄」されます。
For example, if we wanted to fake the LoginContext
class with a fake class for a bunch of related tests, we
would have the following methods in a JUnit test class:
たとえば、一連の関連テスト用に偽のクラスを使用して LoginContext クラスを偽装する場合、JUnit テスト クラスに次のメソッドが含まれます。
public class MyTestClass {
@BeforeClass
public static void applySharedFakes() {
new MockUp<LoginContext>() {
// shared @Mock's here...
@Mock をここで共有しました...
};
}
// test methods that will share the fakes applied above...
上記で適用された偽物を共有するテスト方法...
}
It is also possible to extend from a base test class, which may optionally define "before" methods that apply one or more fakes.
基本テスト クラスから拡張することも可能で、オプションで 1 つ以上のフェイクを適用する "before" メソッドを定義できます。
Sometimes, we may need to apply a fake for the entire scope of a test suite (all of its test classes), ie, a "global" fake.
場合によっては、テスト スイートのスコープ全体 (そのすべてのテスト クラス) にフェイク (つまり、「グローバル」フェイク) を適用する必要があることがあります。
This can be done through external configuration, by setting a system property.
これは、システム プロパティを設定することにより、外部構成を通じて実行できます。
The fakes
system property supports a comma-separated list of fully qualified fake class names.
fakes システム プロパティは、完全修飾された偽クラス名のコンマ区切りリストをサポートします。
If specified at JVM startup time, any such class (which must extend MockUp<T>
) will be automatically applied
for the whole test run.
JVM の起動時に指定すると、そのようなクラス (MockUp<T> を拡張する必要があります) がテスト実行全体に自動的に適用されます。
The fake methods defined in startup fake classes will remain in effect until the end of the test run, for all test classes.
起動フェイククラスで定義されたフェイクメソッドは、すべてのテストクラスに対して、テスト実行が終了するまで有効なままになります。
Each fake class will be instantiated through its no-args constructor, unless an additional value was provided after the class name (for
example, as in "-Dfakes=my.fakes.MyFake=anArbitraryStringWithoutCommas
"), in which case the fake class should have a
constructor with one parameter of type String
.
クラス名の後に追加の値が指定されていない限り (たとえば、「-Dfakes=my.fakes.MyFake=anArbitraryStringWithoutCommas」など)、各フェイク クラスは引数なしのコンストラクターを通じてインスタンス化されます。この場合、フェイク クラスには String 型のパラメーターを 1 つ持つコンストラクターが必要です。
There is one more special @Mock
method that can appear in a fake class: the "$advice
"
method.
偽のクラスに出現できるもう 1 つの特別な @Mock メソッド、「$advice」メソッドがあります。
If defined, this fake method will handle executions of each and every method in the target class (or classes, when applying the fake over
unspecified classes from a base type).
定義されている場合、この偽のメソッドは、ターゲット クラス (または基本型から指定されていないクラスに偽のメソッドを適用する場合はクラス) 内のすべてのメソッドの実行を処理します。
Differently from regular fake methods, this one needs to have a particular signature and return type:
Object $advice(Invocation)
.
通常の偽のメソッドとは異なり、このメソッドには特定のシグネチャと戻り値の型 (Object $advice(Invocation)) が必要です。
For demonstration, lets say we want to measure the execution times of all methods in a given class during test execution, while still
executing the original code of each method.
デモンストレーションとして、各メソッドの元のコードを実行しながら、テスト実行中に特定のクラスのすべてのメソッドの実行時間を測定したいとします。
public final class MethodTiming extends MockUp<Object> {
private final Map<Method, Long> methodTimes = new HashMap<>();
public MethodTiming(Class<?> targetClass) { super(targetClass); }
MethodTiming(String className) throws ClassNotFoundException { super(Class.forName(className)); }
@Mock
public Object $advice(Invocation invocation) {
long timeBefore = System.nanoTime();
try {
return invocation.proceed();
}
finally {
long timeAfter = System.nanoTime();
long dt = timeAfter - timeBefore;
Method executedMethod = invocation.getInvokedMember();
Long dtUntilLastExecution = methodTimes.get(executedMethod);
Long dtUntilNow = dtUntilLastExecution == null ? dt : dtUntilLastExecution + dt;
methodTimes.put(executedMethod, dtUntilNow);
}
}
@Override
protected void onTearDown() {
System.out.println("\nTotal timings for methods in " + targetType + " (ms)");
for (Entry<Method, Long> methodAndTime : methodTimes.entrySet()) {
Method method = methodAndTime.getKey();
long dtNanos = methodAndTime.getValue();
long dtMillis = dtNanos / 1000000L;
System.out.println("\t" + method + " = " + dtMillis);
}
}
}
The fake class above can be applied inside a test, in a "before" method, in a "before class" method, or even for the entire test run by
setting "-Dfakes=testUtils.MethodTiming=my.application.AppClass
".
上記の偽のクラスは、テスト内、「before」メソッド、「before class」メソッド、または「-Dfakes=testUtils.MethodTiming=my.application.AppClass」を設定することでテスト実行全体に適用できます。
It will add up the execution times for all executions of all methods in a given class.
特定のクラス内のすべてのメソッドのすべての実行の実行時間を合計します。
As shown in the implementation of the $advice
method, it can obtain the java.lang.reflect.Method
that is being
executed.
$advice メソッドの実装に示されているように、実行中の java.lang.reflect.Method を取得できます。
If desired, the current invocation count and/or the invocation arguments could be obtained through similar calls to the
Invocation
object.
必要に応じて、Invocation オブジェクトへの同様の呼び出しを通じて、現在の呼び出し回数や呼び出し引数を取得できます。
When the fake is (automatically) torn down, the onTearDown()
method gets executed, dumping measured timings to standard
output.
フェイクが (自動的に) 破棄されると、onTearDown() メソッドが実行され、測定されたタイミングが標準出力にダンプされます。