gpt4 book ai didi

java.lang.LinkageError : loader constraint violation When trying to use Mockito Argument Matcher

转载 作者:行者123 更新时间:2023-11-30 03:26:48 31 4
gpt4 key购买 nike

我正在尝试测试一个类,并且我有自己的参数 Mathcher 来匹配参数。这是代码:

 @Test
public void testDoBindModelsToForm()
{
OrganizationToRelatedSubregionsConverter organizationToRelatedSubregionsConverter = mock(OrganizationToRelatedSubregionsConverter.class);
List<Organization> subregion1 = new ArrayList<Organization>();

Organization subregionOrg1 = CommonentityFactory.eINSTANCE.createOrganization();
subregionOrg1.setMID(OrganizationMID.create(DOMAIN, 100L));
subregionOrg1.setNameFormatted("Subregion 1"); //$NON-NLS-1$
subregion1.add(subregionOrg1);
when(
organizationToRelatedSubregionsConverter.convert(Matchers
.argThat(new OrganizationMIDMatcher(catchmentArea1.getMID())))).thenReturn(
subregion1);

CatchmentFormController catchmentFormController = new CatchmentFormController(
catchmentForm, DOMAIN, conversationDescriptor, configuration, registrationManager,
catchment, null, LaunchMode.ADD, organizationToRelatedSubregionsConverter);

catchmentFormController.renderDynamicForm();
organizationToRelatedSubregionsConverter.convert(catchmentArea1.getMID());
}

这是我的自定义 Matcher 类:

static class OrganizationMIDMatcher extends ArgumentMatcher<OrganizationMID>
{
private OrganizationMID expectedOrganizationMID;

public OrganizationMIDMatcher(OrganizationMID expectedOrganizationMID)
{
this.expectedOrganizationMID = expectedOrganizationMID;
}

@Override
public boolean matches(Object argument)
{
if (argument != null && argument instanceof OrganizationMID)
{
OrganizationMID actualOrganization = (OrganizationMID) argument;
return actualOrganization.equals(expectedOrganizationMID);
}
return false;
}
}

当我运行测试时,出现以下错误。我使用的 Mockito 依赖项是 Mockito-all 1.8.5。

  > java.lang.LinkageError: loader constraint violation:
when resolving method "org.mockito.Matchers.argThat(Lorg/hamcrest/Matcher;)Ljava/lang/Object;"
the class loader (instance of org/eclipse/osgi/internal/baseadaptor/DefaultClassLoader)
of the current class, com/cerner/registration/ui/internal/converters/OrganizationToRelatedSubregionsConverterTest,
and the class loader
(instance of org/eclipse/osgi/internal/baseadaptor/DefaultClassLoader) for resolved class,
org/mockito/Matchers, have different Class objects for the type org/hamcrest/Matcher used in the signature

知道这里可能出现什么问题吗?

最佳答案

您不应将 mockito-alljunit 4.8.2-v370 一起使用。两者都包含 hamcrest 依赖项,并且由于它们在 OSGi 环境中使用不同的类加载器,因此这些类将不兼容。作为解决方法,您应该考虑使用 mockito-core (如果想使用相同版本,则为 1.8.5)并且至少使用 4.9 版本junit (他们开始正确地捆绑 hamcrest:作为依赖项)。

或者 - 不遵循 - 您可以“ fork ”junit4.8.2 版本,并在没有 hamcrest 类的情况下重新打包它.

关于java.lang.LinkageError : loader constraint violation When trying to use Mockito Argument Matcher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30003797/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com