java.lang.LinkageError:尝试使用Mockito Argument Matcher时违反加载程序约束 [英] java.lang.LinkageError: loader constraint violation When trying to use Mockito Argument Matcher

查看:168
本文介绍了java.lang.LinkageError:尝试使用Mockito Argument Matcher时违反加载程序约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to test a class and I have my own argument Mathcher to match the arguments. Here is the code:

 @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类:

And here is my custom Matcher class:

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.

When I run the test, I get the following error. The Mockito dependency I am using is 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

关于这里可能有什么问题的任何想法?

Any idea on what could be the problem here?

推荐答案

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

You should not use mockito-all with junit 4.8.2-v370. Both contain the hamcrest dependency, and as they use different classloaders in an OSGi environment, those classes will be incompatible. As a workaround, you should consider using mockito-core (1.8.5 if want to use the same version) and at least 4.9 version of junit (where they started to bundle hamcrest properly: as a dependency).

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

Alternatively -not to be followed- you can "fork" the 4.8.2 version of junit and repackage it without the hamcrest classes.

这篇关于java.lang.LinkageError:尝试使用Mockito Argument Matcher时违反加载程序约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆