Could not initialize class persistence.common.neo4

2019-07-21 14:44发布

I wanted to see if all my test are passing after git merge, and noticed that sbt test failed. All of those tests are the ones that are using Spring context.

As to not load Spring context for every test I created the following trait:

import org.springframework.context.support.ClassPathXmlApplicationContext

trait Neo4jSpringContext {
  val context = Neo4jSpringContext.context
}

object Neo4jSpringContext {
  val context = new ClassPathXmlApplicationContext("testNeo4jApplicationContext.xml")
}

and in my tests I am using it as follows:

class CreateRetrieveAirplaneAndLegsSpec extends Specification with Neo4jSpringContext with NoTimeConversions

Now, in the logs I can see that the context is being set up (logging information) but then I am facing the error:

[error] Could not create an instance of persistence.list.neo4j.CreateRetrieveAirplaneAndLegsSpec
[error]   caused by java.lang.Exception: Could not instantiate class persistence.list.neo4j.Neo4jDuplicateFlightDesignatorCheckSpec: Could not initialize class persistence.common.neo4j.Neo4jSpringContext$
[error]   org.specs2.reflect.Classes$class.liftedTree1$1(Classes.scala:104)
[error]   org.specs2.reflect.Classes$class.tryToCreateObjectEither(Classes.scala:93)
[error]   org.specs2.reflect.Classes$.tryToCreateObjectEither(Classes.scala:213)
[error]   org.specs2.specification.SpecificationStructure$$anonfun$createSpecificationEither$2.apply(BaseSpecification.scala:118)
[error]   org.specs2.specification.SpecificationStructure$$anonfun$createSpecificationEither$2.apply(BaseSpecification.scala:118)
[error]   scala.Option.getOrElse(Option.scala:120)
[error]   org.specs2.specification.SpecificationStructure$.createSpecificationEither(BaseSpecification.scala:118)
[error]   org.specs2.runner.TestInterfaceRunner.runSpecification(TestInterfaceRunner.scala:59)
[error]   org.specs2.runner.TestInterfaceRunner.run(TestInterfaceRunner.scala:54)
[error]   sbt.RunnerWrapper$1.runRunner(FrameworkWrapper.java:200)
[error]   sbt.RunnerWrapper$1.execute(FrameworkWrapper.java:236)
[error]   sbt.TestRunner.runTest$1(TestFramework.scala:84)
[error]   sbt.TestRunner.run(TestFramework.scala:94)
[error]   sbt.TestFramework$$anon$2$$anonfun$$init$$1$$anonfun$apply$8.apply(TestFramework.scala:224)
[error]   sbt.TestFramework$$anon$2$$anonfun$$init$$1$$anonfun$apply$8.apply(TestFramework.scala:224)
[error]   sbt.TestFramework$.sbt$TestFramework$$withContextLoader(TestFramework.scala:212)
[error]   sbt.TestFramework$$anon$2$$anonfun$$init$$1.apply(TestFramework.scala:224)
[error]   sbt.TestFramework$$anon$2$$anonfun$$init$$1.apply(TestFramework.scala:224)
[error]   sbt.TestFunction.apply(TestFramework.scala:229)
[error]   sbt.Tests$.sbt$Tests$$processRunnable$1(Tests.scala:211)
[error]   sbt.Tests$$anonfun$makeSerial$1.apply(Tests.scala:217)
[error]   sbt.Tests$$anonfun$makeSerial$1.apply(Tests.scala:217)
[error]   sbt.std.Transform$$anon$3$$anonfun$apply$2.apply(System.scala:45)
[error]   sbt.std.Transform$$anon$3$$anonfun$apply$2.apply(System.scala:45)
[error]   sbt.std.Transform$$anon$4.work(System.scala:64)
[error]   sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
[error]   sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
[error]   sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:18)
[error]   sbt.Execute.work(Execute.scala:244)
[error]   sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
[error]   sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
[error]   sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:160)
[error]   sbt.CompletionService$$anon$2.call(CompletionService.scala:30)
[error]   java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
[error]   java.util.concurrent.FutureTask.run(FutureTask.java:166)
[error]   java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
[error]   java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
[error]   java.util.concurrent.FutureTask.run(FutureTask.java:166)
[error]   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
[error]   java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
[error]   java.lang.Thread.run(Thread.java:724)
[error]   caused by java.lang.NoClassDefFoundError: Could not initialize class persistence.common.neo4j.Neo4jSpringContext$
[error]   persistence.common.neo4j.Neo4jSpringContext$class.$init$(Neo4jSpringContext.scala:6)
[error]   persistence.list.neo4j.Neo4jDuplicateFlightDesignatorCheckSpec.<init>(Neo4jDuplicateFlightDesignatorCheckSpec.scala:12)
[error]   sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[error]   sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
[error]   sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[error]   java.lang.reflect.Constructor.newInstance(Constructor.java:526)
[error]   org.specs2.reflect.Classes$class.createInstanceFor(Classes.scala:157)
[error]   org.specs2.reflect.Classes$.createInstanceFor(Classes.scala:213)
[error]   org.specs2.reflect.Classes$$anonfun$createInstanceOfEither$1.apply(Classes.scala:147)
[error]   org.specs2.reflect.Classes$$anonfun$createInstanceOfEither$1.apply(Classes.scala:147)
[error]   scala.Option.map(Option.scala:145)
[error]   org.specs2.reflect.Classes$class.createInstanceOfEither(Classes.scala:147)
[error]   org.specs2.reflect.Classes$.createInstanceOfEither(Classes.scala:213)
[error]   org.specs2.reflect.Classes$class.org$specs2$reflect$Classes$$createInstanceForConstructor(Classes.scala:120)
[error]   org.specs2.reflect.Classes$$anonfun$5.apply(Classes.scala:98)
[error]   org.specs2.reflect.Classes$$anonfun$5.apply(Classes.scala:98)
[error]   scala.collection.GenSeqViewLike$Mapped$class.apply(GenSeqViewLike.scala:48)
[error]   scala.collection.SeqViewLike$$anon$3.apply(SeqViewLike.scala:78)
[error]   scala.collection.GenSeqViewLike$Filtered$class.apply(GenSeqViewLike.scala:91)
[error]   scala.collection.SeqViewLike$$anon$5.apply(SeqViewLike.scala:80)
[error]   scala.collection.GenSeqViewLike$Mapped$class.apply(GenSeqViewLike.scala:48)
[error]   scala.collection.SeqViewLike$$anon$3.apply(SeqViewLike.scala:78)
[error]   org.specs2.reflect.Classes$class.liftedTree1$1(Classes.scala:103)
[error]   org.specs2.reflect.Classes$class.tryToCreateObjectEither(Classes.scala:93)
[error]   org.specs2.reflect.Classes$.tryToCreateObjectEither(Classes.scala:213)
[error]   org.specs2.specification.SpecificationStructure$$anonfun$createSpecificationEither$2.apply(BaseSpecification.scala:118)
[error]   org.specs2.specification.SpecificationStructure$$anonfun$createSpecificationEither$2.apply(BaseSpecification.scala:118)
[error]   scala.Option.getOrElse(Option.scala:120)
[error]   org.specs2.specification.SpecificationStructure$.createSpecificationEither(BaseSpecification.scala:118)
[error]   org.specs2.runner.TestInterfaceRunner.runSpecification(TestInterfaceRunner.scala:59)
[error]   org.specs2.runner.TestInterfaceRunner.run(TestInterfaceRunner.scala:54)
[error]   sbt.RunnerWrapper$1.runRunner(FrameworkWrapper.java:200)
[error]   sbt.RunnerWrapper$1.execute(FrameworkWrapper.java:236)
[error]   sbt.TestRunner.runTest$1(TestFramework.scala:84)
[error]   sbt.TestRunner.run(TestFramework.scala:94)
[error]   sbt.TestFramework$$anon$2$$anonfun$$init$$1$$anonfun$apply$8.apply(TestFramework.scala:224)
[error]   sbt.TestFramework$$anon$2$$anonfun$$init$$1$$anonfun$apply$8.apply(TestFramework.scala:224)
[error]   sbt.TestFramework$.sbt$TestFramework$$withContextLoader(TestFramework.scala:212)
[error]   sbt.TestFramework$$anon$2$$anonfun$$init$$1.apply(TestFramework.scala:224)
[error]   sbt.TestFramework$$anon$2$$anonfun$$init$$1.apply(TestFramework.scala:224)
[error]   sbt.TestFunction.apply(TestFramework.scala:229)
[error]   sbt.Tests$.sbt$Tests$$processRunnable$1(Tests.scala:211)
[error]   sbt.Tests$$anonfun$makeSerial$1.apply(Tests.scala:217)
[error]   sbt.Tests$$anonfun$makeSerial$1.apply(Tests.scala:217)
[error]   sbt.std.Transform$$anon$3$$anonfun$apply$2.apply(System.scala:45)
[error]   sbt.std.Transform$$anon$3$$anonfun$apply$2.apply(System.scala:45)
[error]   sbt.std.Transform$$anon$4.work(System.scala:64)
[error]   sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
[error]   sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
[error]   sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:18)
[error]   sbt.Execute.work(Execute.scala:244)
[error]   sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
[error]   sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
[error]   sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:160)
[error]   sbt.CompletionService$$anon$2.call(CompletionService.scala:30)
[error]   java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
[error]   java.util.concurrent.FutureTask.run(FutureTask.java:166)
[error]   java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
[error]   java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
[error]   java.util.concurrent.FutureTask.run(FutureTask.java:166)
[error]   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
[error]   java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
[error]   java.lang.Thread.run(Thread.java:724)

I was first thinking that there may be some problem because of parallel execution of tests in sbt, but parallelExecution in Test := false didn't help -- the tests keep failing.

Interestingly, if I execute the tests in IntelliJ IDEA everything works and hence face no errors.

EDIT:

name := """scheduling-backend"""

version := "1.0"

scalaVersion := "2.10.2"

resolvers += "spray repo" at "http://repo.spray.io"

resolvers += "spray nightlies" at "http://nightlies.spray.io"

resolvers += "SpringSource Milestone Repository" at "http://repo.springsource.org/milestone"

resolvers += "Neo4j Cypher DSL Repository" at "http://m2.neo4j.org/content/repositories/releases"

libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-actor" % "2.3.0",
  "com.typesafe.akka" %% "akka-slf4j" % "2.3.0",
  "com.typesafe.akka" %% "akka-testkit" % "2.3.0" % "test",
  "com.typesafe.akka" %% "akka-persistence-experimental" % "2.3.0",
  "io.spray" % "spray-can" % "1.3.0",
  "io.spray" % "spray-routing" % "1.3.0",
  "io.spray" % "spray-testkit" % "1.3.0" % "test",
  "io.spray" %% "spray-json" % "1.2.5",
  "ch.qos.logback" % "logback-classic" % "1.0.13",
  "org.specs2" %% "specs2" % "1.14" % "test",
  "com.novocode" % "junit-interface" % "0.7" % "test->default",
  "org.springframework.scala" % "spring-scala" % "1.0.0.M2",
  "org.springframework.data" % "spring-data-neo4j" % "3.0.0.RELEASE",
  "org.springframework.data" % "spring-data-neo4j-rest" % "3.0.0.RELEASE",
  "javax.validation" % "validation-api" % "1.1.0.Final",
  "com.github.nscala-time" %% "nscala-time" % "0.8.0",
  "org.neo4j" % "neo4j-kernel" % "2.0.1" % "test" classifier "tests",
  "com.sun.jersey" % "jersey-core" % "1.9",
  "org.mockito" % "mockito-all" % "1.9.5"
)

scalacOptions ++= Seq(
  "-unchecked",
  "-deprecation",
  "-Xlint",
  "-Ywarn-dead-code",
  "-language:_",
  "-target:jvm-1.7",
  "-encoding", "UTF-8"
)

testOptions += Tests.Argument(TestFrameworks.JUnit, "-v")

1条回答
啃猪蹄的小仙女
2楼-- · 2019-07-21 15:25

UPDATE I had to completely remove my first version of the answer as it turned out completely off base.

The following build.sbt worked fine:

name := "neo4j"

version := "1.0"

scalaVersion := "2.10.4"

// unresolved dependency: org.neo4j#neo4j-cypher-dsl;2.0.1: not found otherwise
resolvers ++= Seq(
  "spring" at "http://repo.spring.io/release",
  "neo4j-releases" at "http://m2.neo4j.org/releases/"
)

libraryDependencies ++= Seq(
  "org.neo4j" % "neo4j-kernel" % "2.1.0-M01" % "test" classifier "tests",
  // https://github.com/sbt/junit-interface/releases
  "com.novocode" % "junit-interface" % "0.11-RC1" % "test",
  "org.hamcrest" % "hamcrest-all" % "1.3" % "test",
  "org.springframework.data" % "spring-data-neo4j" % "3.0.1.RELEASE" % "test",
  "org.springframework" % "spring-test" % "4.0.2.RELEASE" % "test"
)

// http://www.scala-sbt.org/release/docs/Detailed-Topics/Testing#options
testOptions += Tests.Argument(TestFrameworks.JUnit, "-v")

It worked fine with the following unit test described in http://docs.neo4j.org/chunked/stable/tutorials-java-unit-testing.html (as pointed out in @Andna's comment).

/**
 * Licensed to Neo Technology under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Neo Technology licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.neo4j.examples;

import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.test.TestGraphDatabaseFactory;

/**
 * An example of unit testing with Neo4j.
 */
public class Neo4jBasicDocTest
{
    protected GraphDatabaseService graphDb;

    /**
     * Create temporary database for each unit test.
     */
    // START SNIPPET: beforeTest
    @Before
    public void prepareTestDatabase()
    {
        graphDb = new TestGraphDatabaseFactory().newImpermanentDatabase();
    }
    // END SNIPPET: beforeTest

    /**
     * Shutdown the database.
     */
    // START SNIPPET: afterTest
    @After
    public void destroyTestDatabase()
    {
        graphDb.shutdown();
    }
    // END SNIPPET: afterTest

    @Test
    public void startWithConfiguration()
    {
        // START SNIPPET: startDbWithConfig
        GraphDatabaseService db = new TestGraphDatabaseFactory()
            .newImpermanentDatabaseBuilder()
            .setConfig( GraphDatabaseSettings.nodestore_mapped_memory_size, "10M" )
            .setConfig( GraphDatabaseSettings.string_block_size, "60" )
            .setConfig( GraphDatabaseSettings.array_block_size, "300" )
            .newGraphDatabase();
        // END SNIPPET: startDbWithConfig
        db.shutdown();
    }

    @Test
    public void shouldCreateNode()
    {
        // START SNIPPET: unitTest
        Node n = null;
        try ( Transaction tx = graphDb.beginTx() )
        {
            n = graphDb.createNode();
            n.setProperty( "name", "Nancy" );
            tx.success();
        }

        // The node should have a valid id
        assertThat( n.getId(), is( greaterThan( -1L ) ) );

        // Retrieve a node by using the id of the created node. The id's and
        // property should match.
        try ( Transaction tx = graphDb.beginTx() )
        {
            Node foundNode = graphDb.getNodeById( n.getId() );
            assertThat( foundNode.getId(), is( n.getId() ) );
            assertThat( (String) foundNode.getProperty( "name" ), is( "Nancy" ) );
        }
        // END SNIPPET: unitTest
    }
}

I'm experimenting...

查看更多
登录 后发表回答