Junit EasyMock在创建构造函数并引发NullPointerException时调用hashcode

Junit EasyMock在创建构造函数并引发NullPointerException时调用hashcode,junit,nullpointerexception,easymock,Junit,Nullpointerexception,Easymock,我不明白为什么EasyMock会在为属性赋值之前自动调用hashcode()。当我在测试类中创建构造函数时,它不会设置属性的值,而是调用被重写的hashcode()并抛出NullPointerException。 我将EasyMock与PowerMock结合用于Junittest 代码如下所示: Class Customer{ /** * Creating a constructor */ public Customer(String name, String familyName, B b)

我不明白为什么EasyMock会在为属性赋值之前自动调用hashcode()。当我在测试类中创建构造函数时,它不会设置属性的值,而是调用被重写的hashcode()并抛出NullPointerException。 我将EasyMock与PowerMock结合用于Junittest

代码如下所示:

Class Customer{
/**
* Creating a constructor
*/
 public Customer(String name, String familyName, B b) throws IllegalArgumentException{
         initAttr(name, familyName, b);
 }

 private void initAttr(String name, String familyName, B b) throws IllegalArgumentException{
        if (name == null || familyName == null || b == null)
              throw new  IllegalArgumentException("Invalid input");
         this.name = name;
         this.familyName = familyName;
         this.b = b;


 }

/**
*Test will call this method before setting value for properties
* so now : name and familyName is null-> throw an exception
**/
@Override
  public int hashCode() {
      // Very simple approach:
   // Using Joshua Bloch's recipe:
  int result = 17;
  result = 37 * result + this.name.hashCode();
  result = 37 * result + this.familyName.hashCode();
  return result;
}

}
我实现了一个测试类:

@RunWith(PowerMockRunner.class)
@PrepareForTest({Customer.class, Datutil.class})

class TestCustomer{

        void setUp{

        mockB = createMock(B.class)

       }



        @Test
       public void testConstructor(){

       //do replay()...<br/>

        a = new Customer("name","faname", mockB);//the error happens here

        //do verify()...<br/>

    }

    }
@RunWith(PowerMockRunner.class)
@PrepareForTest({Customer.class,Datutil.class})
类TestCustomer{
无效设置{
mockB=createMock(B.class)
}
@试验
公共void testConstructor(){
//执行replay()…
a=新客户(“名称”、“faname”、mockB);//错误发生在这里 //进行验证()…
} }
非常感谢

以下是错误日志:

java.lang.NullPointerException
    at lib.customer.Customer.hashCode(Customer.java:253)
    at java.util.HashMap.hash(Unknown Source)
    at java.util.HashMap.getEntry(Unknown Source)
    at java.util.HashMap.get(Unknown Source)
    at org.powermock.core.MockRepository.getInstanceMethodInvocationControl(MockRepository.java:136)
    at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:57)
    at org.powermock.core.MockGateway.methodCall(MockGateway.java:84)
    at lib.customer.Customer.initAttr(Customer.java)
    at lib.customer.Customer.<init>(Customer.java:96)
    at test.lib.customer.TestCustomer.testConstructor(TestCustomer.java:122)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$2.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:217)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:88)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96)
java.lang.NullPointerException
位于lib.customer.customer.hashCode(customer.java:253)
位于java.util.HashMap.hash(未知源)
位于java.util.HashMap.getEntry(未知源)
位于java.util.HashMap.get(未知源)
位于org.powermock.core.MockRepository.getInstanceMethodInvocationControl(MockRepository.java:136)
位于org.powermock.core.MockGateway.doMethodCall(MockGateway.java:57)
位于org.powermock.core.MockGateway.methodCall(MockGateway.java:84)
位于lib.customer.customer.initAttr(customer.java)
在lib.customer.customer.(customer.java:96)
位于test.lib.customer.TestCustomer.testConstructor(TestCustomer.java:122)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(未知源)
在sun.reflect.DelegatingMethodAccessorImpl.invoke处(未知源)
位于java.lang.reflect.Method.invoke(未知源)
位于org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
位于org.powermock.modules.junit4.internal.impl.powermockjunit44runnerdelegateinpl$2.runTestMethod(powermockjunit44runnerdelegateinpl.java:217)
位于org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:88)
位于org.junit.internal.runners.MethodRoadie.runbeforesthentestthenuter(MethodRoadie.java:96)

我猜A和客户实际上是同一类人?请向我们显示异常的完整堆栈跟踪。您没有嘲笑过一个/客户,因此Easymock有理由调用
hashCode()
。您是对的。很抱歉我刚刚添加了错误日志。错误发生在Client.hashCode()中。这个新班级是什么?如果您正在显示的代码与错误消息指示的内容不匹配,则无法回答此问题。创建一个重现问题的SSCCE,并向我们展示SSCCEsorry@JBNizet的真实完整代码。对于缺失,我更新了错误日志,但它没有正常工作。现在我又做了一次。您仍然没有提供类B2的代码。您应该使hashCode方法为null安全,或者只使用apachecommons,它的功能与您完全相同