代码之家  ›  专栏  ›  技术社区  ›  Sarah Messer

MeanBean EqualMethodTester和HashCodeMethodTester的自定义属性工厂

  •  1
  • Sarah Messer  · 技术社区  · 7 年前

    是否可以配置自定义工厂以生成 EqualsMethodTester HashCodeMethodTester 来自的类 org.meanbean.test ? 当我通过适用于的配置时 BeanTester 等效方法测试仪 ,我在错误回溯中得到以下消息:

    组织。meanbean。工厂。ObjectCreationException:未能为属性[demoUrl]创建值。

    未能为类型为[class java.net.URL]的属性[demoUrl]找到合适的工厂。请注册一家定制工厂。

    组织。meanbean。工厂。ObjectCreationException:由于NoSuchMethodException,未能实例化[java.net.URL]类型的对象。

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

    (两者 等效方法测试仪 HashCodeMethodTester 给出此错误。将“demoUrl”添加到 insignificantProperties 对于 EqualsMethodTester().testEqualsMethod()

    我没有看到任何将配置传递到的选项 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

    (我使用的是MeanBeanV2.0.3和Java1.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;
        }
    
    }
    

    meanbean: failed to test bean with arrays 它适用于 BeanTester公司 但不是为了 等效方法测试仪 :

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

    我错过了什么?

    1 回复  |  直到 7 年前
        1
  •  2
  •   perbellinio    7 年前

    看起来像是 EqualsMethodTester().testEqualsMethod() 由于使用原因,在特定情况下需要等效工厂 java.net.URL 它不提供默认的空构造函数。那么什么时候 BasicNewObjectInstanceFactory.create() 需要 呼叫 clazz.getDeclaredConstructor() 引发异常 Method threw 'java.lang.NoSuchMethodException' exception. . 匿名实现可以是: 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: 新的EqualMethodTester()。TestEqualMethod(productEquivalentFactory,configureMeanBeanTests(),“demoUrl”)`

    我测试了它,它正在工作。