web-dev-qa-db-ja.com

Javaクラスで例外という名前の@InjectMocksフィールドをインスタンス化できません

ユーザー定義のコンストラクタを持つクラスがあります。

public class Employee   
{
    @Inject
    private MyBean myBean;

    private String abcd;    

    protected Employee(Parameter1 param1, Parameter2 param2)
    { //some operations on method params
    //some operation on mybean
      this.abcd = "some value";
    }

    protected String getAbcd()
    {
        return nrOfAccesses;
    }

    protected void setAbcd(String abcd)
    {
        this.abcd = abcd;
    }

}

テストクラス

@RunWith(MockitoJUnitRunner.class)
public class TestEmployee
{


    @Mock
    private MyBean myBean;

    private Parameter1 param1;
    private Parameter2 param2;

    @InjectMocks
    private Employee employee;


    @Before
    public void prepare()
        throws Exception
    {
        //some intialization
        param1 = some value;
        param2 = some value;            
        when(myBean.get(eq("ID"))).thenReturn("1075");

    }

    @Test
    public void testEmployeeID()
    {
        employee = new Employee(param1, param2);
        assertThat(employee.getAbcd(), is("XYZC"));        
    }

私は例外を得ています

org.mockito.exceptions.base.MockitoException: 
Cannot instantiate @InjectMocks field named 'employee' of type 'class com.xyz.Employee'.
You haven't provided the instance at field declaration so I tried to construct the instance.
However the constructor or the initialization block threw an exception : null

    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl$1.withBefores(JUnit45AndHigherRunnerImpl.Java:27)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.Java:254)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.Java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.Java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.Java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.Java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.Java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.Java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.Java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.Java:309)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.Java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.Java:62)
    at org.Eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.Java:50)
    at org.Eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.Java:38)
    at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.Java:467)
    at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.Java:683)
    at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.Java:390)
    at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.Java:197)
Caused by: Java.lang.NullPointerException
14
Patan

employee = new Employee(param1, param2);を実行する場合は、@InjectMocksをスキップすることもできます。

次のことを行うことになっています。

@InjectMocks
ClassUnderTest cut;

@Mock
Dependency1 dep1;
@Mock
Dependency2 dep2;

@Before
public void setup() {
  initMocks(this);
}

@InjectMocksを省略すると、次のコードで同じ動作を実現できます。

ClassUnderTest cut;

@Mock
Dependency1 dep1;
@Mock
Dependency2 dep2;

@Before
public void setup() {
  initMocks(this);
  cut = new ClassUnderTest(dep1, dep2);
}

特定のケースでは、param1param2をモックする必要があります。 @InjectMocksを使用するときは、コンストラクタを手動で呼び出さないでください。

17
Max Fichtelmann