找到你要的答案

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

Q:java.lang.linkageerror:装载约束违反当试图使用mockito参数匹配

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());
}

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;
    }
}

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?

我想测试一个类,我有我自己的观点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());
}

这是我的自定义匹配类:

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所有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

有什么想法吗?

answer1: 回答1:

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).

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

你不应该使用mockito所有JUnit 4.8.2-v370。既包含hamcrest依赖,当他们使用不同的类加载器在OSGi环境中,这些类将不兼容。作为一种替代方法,您应该考虑使用mockito核心(1.8.5如果想使用相同的版本)和至少4.9个版本的JUnit(他们开始束hamcrest正确:作为一个依赖)。

或者不跟着你可以“叉”的超级版本的JUnit和重新包装没有hamcrest类。

java  mockito  linkageerror