未能模拟@Autowired Object

时间:2017-07-21 12:36:31

标签: java junit mockito junit4 powermock

我正在使用Junit4和Mockito进行测试用例,在下面的代码中我试图模拟一个自动对象,该对象在模拟类中抛出空指针异常,这意味着自动对象没有正确模拟

ContentDao.java

public class ContentDao {

  @Autowired 
  private ConfigProperties configProperties;

  public void fuction() {
  int batchSize = configProperties.getBatchSize();
}

ConfigProperties.java

@ConfigurationProperties(ignoreUnknownFields = false, prefix = "cleanup")
public class ConfigProperties {

  private int batchSize;

  public int getBatchSize() {
    return batchSize;
  }
}

尝试模拟ConfigProperties。

@RunWith(MockitoJUnitRunner.class)
public class ContentDaoTest{

   @InjectMocks
   private ContentDao contentDao;

   @Mock
   private ConfigProperties configProperties;

   @Test
   public void functionTest(){
      configProperties = mock(ConfigProperties.class);
      when(configProperties.getBatchSize()).thenReturn(100);
      ContentDao contentDao = new ContentDao();
      contentDao.funtion();
   }
调用了函数,但我在下面的行中获得了NPE。请帮助我被困在这里。

int batchSize = configProperties.getBatchSize();

1 个答案:

答案 0 :(得分:4)

如果为configProperties设置[site@x neo4j]$ neo4jstart-234.sh [site@x neo4j]$ WARNING: Max 1024 open files allowed, minimum of 40 000 recommended. See the Neo4j manual. Starting Neo4j Server console-mode... 2017-07-20 10:37:51.073-0400 INFO Successfully shutdown Neo4j Server 2017-07-20 10:37:51.074-0400 ERROR Failed to start Neo4j: Starting Neo4j failed: Component 'org.neo4j.server.database.LifecycleManagingDatabase@79b877d2' was successfully initialized, but failed to start. Please see attached cause exception. Starting Neo4j failed: Component 'org.neo4j.server.database.LifecycleManagingDatabase@79b877d2' was successfully initialized, but failed to start. Please see attached cause exception. org.neo4j.server.ServerStartupException: Starting Neo4j failed: Component 'org.neo4j.server.database.LifecycleManagingDatabase@79b877d2' was successfully initialized, but failed to start. Please see attached cause exception. at org.neo4j.server.exception.ServerStartupErrors.translateToServerStartupError(ServerStartupErrors.java:67) at org.neo4j.server.AbstractNeoServer.start(AbstractNeoServer.java:235) at org.neo4j.server.Bootstrapper.start(Bootstrapper.java:97) at org.neo4j.server.CommunityBootstrapper.start(CommunityBootstrapper.java:48) at org.neo4j.server.CommunityBootstrapper.main(CommunityBootstrapper.java:35) Caused by: org.neo4j.kernel.lifecycle.LifecycleException: Component 'org.neo4j.server.database.LifecycleManagingDatabase@79b877d2' was successfully initialized, but failed to start. Please see attached cause exception. at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:462) at org.neo4j.kernel.lifecycle.LifeSupport.start(LifeSupport.java:111) at org.neo4j.server.AbstractNeoServer.start(AbstractNeoServer.java:195) ... 3 more Caused by: java.lang.RuntimeException: Error starting org.neo4j.kernel.impl.factory.CommunityFacadeFactory, /scratch/neo4j/neo4j-community-2.3.4/data/graph.db at org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory.newFacade(GraphDatabaseFacadeFactory.java:143) at org.neo4j.kernel.impl.factory.CommunityFacadeFactory.newFacade(CommunityFacadeFactory.java:43) at org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory.newFacade(GraphDatabaseFacadeFactory.java:108) at org.neo4j.server.CommunityNeoServer$1.newGraphDatabase(CommunityNeoServer.java:66) at org.neo4j.server.database.LifecycleManagingDatabase.start(LifecycleManagingDatabase.java:95) at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:452) ... 5 more Caused by: org.neo4j.kernel.lifecycle.LifecycleException: Component 'org.neo4j.kernel.NeoStoreDataSource@14c7f72f' was successfully initialized, but failed to start. Please see attached cause exception. at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:462) at org.neo4j.kernel.lifecycle.LifeSupport.start(LifeSupport.java:111) at org.neo4j.kernel.impl.transaction.state.DataSourceManager.start(DataSourceManager.java:112) at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:452) at org.neo4j.kernel.lifecycle.LifeSupport.start(LifeSupport.java:111) at org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory.newFacade(GraphDatabaseFacadeFactory.java:139) ... 10 more Caused by: org.neo4j.kernel.impl.store.UnderlyingStorageException: Unable to open store file: /scratch/neo4j/neo4j-community-2.3.4/data/graph.db/neostore.nodestore.db.labels at org.neo4j.kernel.impl.store.CommonAbstractStore.checkStorage(CommonAbstractStore.java:161) at org.neo4j.kernel.impl.store.CommonAbstractStore.initialise(CommonAbstractStore.java:102) at org.neo4j.kernel.impl.store.NeoStores.initialize(NeoStores.java:429) at org.neo4j.kernel.impl.store.NeoStores$StoreType$1.open(NeoStores.java:85) at org.neo4j.kernel.impl.store.NeoStores$StoreType$1.open(NeoStores.java:78) at org.neo4j.kernel.impl.store.NeoStores.openStore(NeoStores.java:422) at org.neo4j.kernel.impl.store.NeoStores.getOrCreateStore(NeoStores.java:467) at org.neo4j.kernel.impl.store.NeoStores.<init>(NeoStores.java:365) at org.neo4j.kernel.impl.store.StoreFactory.openNeoStores(StoreFactory.java:174) at org.neo4j.kernel.impl.store.StoreFactory.openAllNeoStores(StoreFactory.java:138) at org.neo4j.kernel.NeoStoreDataSource.buildNeoStore(NeoStoreDataSource.java:661) at org.neo4j.kernel.NeoStoreDataSource.start(NeoStoreDataSource.java:535) at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:452) ... 15 more Caused by: org.neo4j.io.pagecache.impl.FileLockException: Externally locked: /scratch/neo4j/neo4j-community-2.3.4/data/graph.db/neostore.nodestore.db.labels at org.neo4j.io.pagecache.impl.SingleFilePageSwapper.acquireLock(SingleFilePageSwapper.java:209) at org.neo4j.io.pagecache.impl.SingleFilePageSwapper.<init>(SingleFilePageSwapper.java:160) at org.neo4j.io.pagecache.impl.SingleFilePageSwapperFactory.createPageSwapper(SingleFilePageSwapperFactory.java:64) at org.neo4j.io.pagecache.impl.muninn.MuninnPagedFile.<init>(MuninnPagedFile.java:95) at org.neo4j.io.pagecache.impl.muninn.MuninnPageCache.map(MuninnPageCache.java:357) at org.neo4j.kernel.impl.store.CommonAbstractStore.checkStorage(CommonAbstractStore.java:140) ... 27 more ,则不应再次模拟@Mock 同样的想法,当你为contentDao设置configProperties = mock(ConfigProperties.class);时,你不应该实例化一个新的contentDao。

@InjectMocks