本文介绍了MeanBean EqualsMethodTester和HashCodeMethodTester的自定义属性工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以配置自定义工厂以为 org.meanbean.test 中的 EqualsMethodTester HashCodeMethodTester 类生成值?当我将适用于 BeanTester 的配置传递给 EqualsMethodTester 时,我在错误回溯中收到以下消息:

Is it possible to configure custom factories to generate values for the EqualsMethodTester and HashCodeMethodTester classes from org.meanbean.test? When I pass the Configuration which works for BeanTester to EqualsMethodTester, I get the following messages in the error traceback:

org.meanbean.factories.ObjectCreationException:无法为属性[demoUrl]创建值.

未能为类型= [class java.net.URL]的property = [demoUrl]找到合适的Factory.请注册自定义工厂.

org.meanbean.factories.ObjectCreationException:由于NoSuchMethodException而无法实例化[java.net.URL]类型的对象.

java.lang.NoSuchMethodException: java.net.URL.< init>()

java.lang.NoSuchMethodException: java.net.URL.<init>()

(( EqualsMethodTester HashCodeMethodTester 都会出现此错误.在 EqualsMethodTester()的 insignificantProperties 列表中添加"demoUrl".testEqualsMethod()没什么区别.单步执行代码意味着我的URLFactory.create()根本没有被调用.)

(Both EqualsMethodTester and HashCodeMethodTester give this error. Adding "demoUrl" to the list of insignificantProperties for EqualsMethodTester().testEqualsMethod() makes no difference. Stepping through the code implies my URLFactory.create() isn't called at all.)

我看不到任何将配置传递到 HashCodeMethodTester 的选项.我在以下站点上浏览了文档,但找不到解决方案或对缺少的功能的认可: http://meanbean.sourceforge.net/docs/2.0.3/public/org/meanbean/test/EqualsMethodTester.html http://meanbean.sourceforge.net/docs/2.0.3/public/org/meanbean/test/HashCodeMethodTester.html http://meanbean.sourceforge.net/docs/2.0.3/public/org/meanbean/test/ConfigurationBuilder.html http://meanbean.sourceforge.net/docs/2.0.3/Mean_Bean_2.0.3_User_Guide.pdf

I do not see any options for passing the configuration into HashCodeMethodTester. I've skimmed documentation at the following sites, but have found neither a solution nor acknowledgement of the missing functionality: http://meanbean.sourceforge.net/docs/2.0.3/public/org/meanbean/test/EqualsMethodTester.htmlhttp://meanbean.sourceforge.net/docs/2.0.3/public/org/meanbean/test/HashCodeMethodTester.htmlhttp://meanbean.sourceforge.net/docs/2.0.3/public/org/meanbean/test/ConfigurationBuilder.htmlhttp://meanbean.sourceforge.net/docs/2.0.3/Mean_Bean_2.0.3_User_Guide.pdf

(我正在使用MeanBean v 2.0.3和Java 1.8.)

(I'm using MeanBean v 2.0.3 and Java 1.8.)

我有以下课程,使用 java.net.URL :

public class Product {
    private String name;
    private URL demoUrl;

    public Product(){
        super();
    }

    public int hashCode() {
        return Objects.hash(getName(), whitehawkSKU);
    }

    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (obj.getClass() != getClass()) {
            return false;
        }
        Product other = (Product) obj;
        return Objects.equals(getName(), other.getName());
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public URL getDemoUrl() {
        return demoUrl;
    }

    public void setDemoUrl(URL demoUrl) {
        this.demoUrl = demoUrl;
    }

}

要处理URL字段,我根据 meanbean创建了一个自定义工厂:无法通过数组测试bean ,它适用于 BeanTester ,但不适用于 EqualsMethodTester :

To handle the URL field, I created a custom factory, as per meanbean: failed to test bean with arrays and it works for BeanTester but not for EqualsMethodTester:

import org.meanbean.lang.Factory;

import java.net.MalformedURLException;
import java.net.URL;

public class URLFactory implements Factory<URL> {
    private static int counter = 0;

    @Override
    public URL create() {
        String host = "http://test." + counter + ".url/";
        try {
            return new URL(host);
        }
        catch (MalformedURLException except) {
            return null;
        }
    }
}

我的测试方法如下:

private Configuration configureMeanBeanTests() {
    URLFactory urlFactory = new URLFactory();
    return new ConfigurationBuilder()
        .overrideFactory("demoUrl", urlFactory).build();
}

@Test
public void testAccessors() {
    new BeanTester().testBean(Product.class, configureMeanBeanTests());
}

@Test
public void testEquals() {
    new EqualsMethodTester().testEqualsMethod(
        Product.class,
        configureMeanBeanTests(),
        "name",
        "demoUrl"
    );
}

@Test
public void testHashCode() {
    new HashCodeMethodTester().testHashCodeMethod(Product.class);
}

我想念什么?

推荐答案

在特定情况下,由于使用 java, EqualsMethodTester().testEqualsMethod()看起来需要EquivalentFactory.net.URL 不提供默认的空构造函数.因此,当为 java.net.URL 调用 BasicNewObjectInstanceFactory.create()时,调用 clazz.getDeclaredConstructor()会引发异常方法抛出"java.lang.NoSuchMethodException"异常..基本上,您只需要实现一个EquivalentFactory.匿名实现可以是: private EquivalentFactory< Product>productEquivalentFactory =新的EquivalentFactory< Product>(){@Override公开产品create(){产品p =新产品();尝试 {p.setDemoUrl(new URL("http://test.1.url/"));;} catch(MalformedURLException e){e.printStackTrace();}p.setName("test");返回p;}};它必须与您已经拥有的自定义配置一起使用: new EqualsMethodTester().testEqualsMethod(productEquivalentFactory,configureMeanBeanTests(),"demoUrl");`

It looks like the EqualsMethodTester().testEqualsMethod() needs a EquivalentFactory in that particular case due to the use java.net.URL that does not provide a default empty constructor. So when BasicNewObjectInstanceFactory.create() is called for java.net.URL the call the clazz.getDeclaredConstructor() throw an exception Method threw 'java.lang.NoSuchMethodException' exception..Basically you just have to implement a EquivalentFactory.An anonymous implementation could be:private EquivalentFactory<Product> productEquivalentFactory = new EquivalentFactory<Product>() { @Override public Product create() { Product p = new Product(); try { p.setDemoUrl(new URL("http://test.1.url/")); } catch (MalformedURLException e) { e.printStackTrace(); } p.setName("test"); return p; } };It has to be used with the custom configuration that you already have:new EqualsMethodTester().testEqualsMethod(productEquivalentFactory, configureMeanBeanTests(), "demoUrl");`

对于哈希码,只需使用等效的工厂即可.

For the hashcode just use the equivalent factory and it does the job.

我对其进行了测试,并且可以正常工作.

I tested it and it is working.

这篇关于MeanBean EqualsMethodTester和HashCodeMethodTester的自定义属性工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 11:36