Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java org.mockito.exceptions.misusing.InjectMocksException:无法实例化@InjectMocks_Java_Spring Boot_Junit_Mockito - Fatal编程技术网

Java org.mockito.exceptions.misusing.InjectMocksException:无法实例化@InjectMocks

Java org.mockito.exceptions.misusing.InjectMocksException:无法实例化@InjectMocks,java,spring-boot,junit,mockito,Java,Spring Boot,Junit,Mockito,我使用@InjectMocks将存储库实现注入到我的测试类中,但它抛出InjectMocksException。下面是我的代码和错误,请帮助如何解决此错误 错误: org.mockito.exceptions.misusing.InjectMocksException: Cannot instantiate @InjectMocks field named 'muRepository' of type 'class com.example.MyrRepositoryImpl'. You hav

我使用
@InjectMocks
将存储库实现注入到我的测试类中,但它抛出
InjectMocksException
。下面是我的代码和错误,请帮助如何解决此错误

错误:

org.mockito.exceptions.misusing.InjectMocksException: 
Cannot instantiate @InjectMocks field named 'muRepository' of type 'class com.example.MyrRepositoryImpl'.
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.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:44)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:77)
    at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:83)
    at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
    at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: java.lang.NullPointerException
    at com.example.MyRepositoryImpl.<init>(MyRepositoryImpl.java:27)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
@InjectMocks
private MyRepositoryImpl myRepository;
@Test (expected = NullPointerException.class )
public void procTest( ) throws Exception
{
        when( addProc.execute( null,
                               null,
                               null
                              )
            ).thenThrow( new NullPointerException() );

        myRepository.addUser( null );
}
public class MyRepositoryImpl implements MyRepository
{
    private final AddProc addProc;

    @Autowired
    public MyRepositoryImpl( final ProcFactory procFactory )
    {
        this.addProc = procFactory.create( AddProc.class ); //this is line 27 referenced in error
    }

    @Override
    public User addUser( User user) 
    {
            return addProc.execute(
                                     user.getUserName( ),
                                     user.getFirstName( ),
                                     user.getLastName( )
                                   );


        }
}
测试:

org.mockito.exceptions.misusing.InjectMocksException: 
Cannot instantiate @InjectMocks field named 'muRepository' of type 'class com.example.MyrRepositoryImpl'.
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.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:44)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:77)
    at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:83)
    at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
    at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: java.lang.NullPointerException
    at com.example.MyRepositoryImpl.<init>(MyRepositoryImpl.java:27)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
@InjectMocks
private MyRepositoryImpl myRepository;
@Test (expected = NullPointerException.class )
public void procTest( ) throws Exception
{
        when( addProc.execute( null,
                               null,
                               null
                              )
            ).thenThrow( new NullPointerException() );

        myRepository.addUser( null );
}
public class MyRepositoryImpl implements MyRepository
{
    private final AddProc addProc;

    @Autowired
    public MyRepositoryImpl( final ProcFactory procFactory )
    {
        this.addProc = procFactory.create( AddProc.class ); //this is line 27 referenced in error
    }

    @Override
    public User addUser( User user) 
    {
            return addProc.execute(
                                     user.getUserName( ),
                                     user.getFirstName( ),
                                     user.getLastName( )
                                   );


        }
}
存储库:

org.mockito.exceptions.misusing.InjectMocksException: 
Cannot instantiate @InjectMocks field named 'muRepository' of type 'class com.example.MyrRepositoryImpl'.
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.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:44)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:77)
    at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:83)
    at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
    at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: java.lang.NullPointerException
    at com.example.MyRepositoryImpl.<init>(MyRepositoryImpl.java:27)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
@InjectMocks
private MyRepositoryImpl myRepository;
@Test (expected = NullPointerException.class )
public void procTest( ) throws Exception
{
        when( addProc.execute( null,
                               null,
                               null
                              )
            ).thenThrow( new NullPointerException() );

        myRepository.addUser( null );
}
public class MyRepositoryImpl implements MyRepository
{
    private final AddProc addProc;

    @Autowired
    public MyRepositoryImpl( final ProcFactory procFactory )
    {
        this.addProc = procFactory.create( AddProc.class ); //this is line 27 referenced in error
    }

    @Override
    public User addUser( User user) 
    {
            return addProc.execute(
                                     user.getUserName( ),
                                     user.getFirstName( ),
                                     user.getLastName( )
                                   );


        }
}

Mockito使用constroctor注入,但您没有为ProcFactory类使用mock对象。 因此出现空指针异常。您必须在测试类中将ProcFactory用作模拟,或者必须添加null检查

@Mock
private ProcFactory  procFactory;

鉴于

已经看到您正在JUnit4上运行测试,为了通过
@InjectMocks
初始化和注入mock,需要在设置步骤中将
@RunWith(MockitoJUnitRunner.class)
用作测试运行程序或
Mockito.initMocks(this)

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
}

要么你忘了给我们看,要么你忘了为ProcFactory创建一个模拟我已经在这里创建了它