単体テストの目的で、javax.security.auth.kerberos.KerberosTicketインスタンスのモックを提供したいと思います。MockitoとPowerMockitoを使用してみましたが、どちらも同様のメッセージで失敗しました。
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
2. inside when() you don't call method on mock but on some other object.
@RunWithおよび@PrepareForTestアノテーションを追加すると、次のLinkageExceptionが発生します。
java.lang.LinkageError:
loader constraint violation:
when resolving method "sun.security.jgss.krb5.Krb5Util.getTicketFromSubjectAndTgs(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/security/AccessControlContext;)Ljavax/security/auth/kerberos/KerberosTicket;"
the class loader (instance of org/powermock/core/classloader/MockClassLoader) of the current class,
org/epo/security/spnego/impl/KerberosUtilsTest, and the class loader (instance of <bootloader>)
for resolved class, sun/security/jgss/krb5/Krb5Util, have different Class objects
for the type javax/security/auth/kerberos/KerberosTicket used in the signature
私のテストクラスは次のようになります。
@RunWith(PowerMockRunner.class)
@PrepareForTest({KerberosTicket.class})パブリッククラスKerberosUtilsTest {
@Test
public void testGetTicketFromSubjectAndTgs() throws LoginException, KrbException, IOException {
PowerMockito.mockStatic(Krb5Util.class);
KerberosTicket kerberosTicketMock = PowerMockito.mock(KerberosTicket.class);
AccessControlContext controlContext = AccessController.getContext();
// Causes the LinkageError to be thrown
when(Krb5Util.getTicketFromSubjectAndTgs(-1, "test", "test", "test", controlContext)).thenReturn(
kerberosTicketMock);
KerberosTicket actual = new KerberosUtils().getTicketFromSubjectAndTgs(-1, "test", "test", "test",
controlContext);
assertEquals(kerberosTicketMock, actual);
}
インターネットを検索すると、注釈を次のように変更することでLinkageErrorを修正できることがわかりました。
@PowerMockIgnore({"sun.security.jgss.krb5.Krb5Util", "javax.security.auth.kerberos.KerberosTicket"})
@PrepareForTest({KerberosUtils.class})
@RunWith(PowerMockRunner.class)
これを実行すると、正方形に戻ります。
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
2. inside when() you don't call method on mock but on some other object.
誰かがこれに対する解決策を持っていますか?