无法使用 java 类实例化名为 exception @InjectMocks 字段

2022-09-01 15:53:52

我有一个带有用户定义构造函数的类。

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

答案 1

如果你做一个,你不妨跳过.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


答案 2

这可以通过遵循我的解决方案来解决

@ExtendWith(MockitoExtension.class)
public class EmployeeServiceTests {

    @Mock
    private EmployeeRepository repository;
    
    @InjectMocks
    private EmployeeService service = new EmployeeServiceImpl(repository); // need to declare an appropriate  constructor in the EmployeeServiceImpl , 
    
    private Employee employee;
    

    @Test
    public void whenSaveThenReturnSavedObject() {
        Employee employee = Employee.builder().firstName("Vineeth").lastName("A M").email("Myemail").build();
        Mockito.when(repository.save(employee)).thenReturn(employee);
        Employee savedEmp = service.saveEmployee(employee);
        assertEquals(savedEmp.getEmail(),employee.getEmail());
    }
}


@Service
public class EmployeeServiceImpl implements EmployeeService {


private EmployeeRepository employeeRepository;
    

    @Autowired
    public EmployeeServiceImpl(EmployeeRepository employeeRepository) {
    super();
    this.employeeRepository = employeeRepository;
}


    @Override
    public Employee saveEmployee(Employee obj) {
        Employee employee = employeeRepository.save(obj);
        return employee;
    }

}

推荐