not acessible via JUnit
I have the following class:
package backend.link
import org.bson.types.ObjectId
import com.novus.salat.annotations.raw.Salat
import com.novus.salat.dao.ModelCompanion
import com.novus.salat.dao.SalatDAO
import model.hybrid.HybridEntity
import play.api.Play.current
import se.radley.plugin.salat.mongoCollection
import com.novus.salat.annotations.raw.Key
import backend.data.MongoDBLayer
import com.mongodb.casbah.commons.MongoDBObject
import backend.data.SpaceDao
import backend.data.PageDao
import backend.data.UserDao
import se.radley.plugin.salat._
import org.bson.types.ObjectId
import com.novus.salat.{ TypeHintFrequency, StringTypeHintStrategy, Context }
import play.api.Play
import play.api.Play.current
/*
Adding a custom Salat context to work with Play's Classloader
Using example from:
https://github.com/leon/play-salat/blob/master/sample/app/models/mongoContext.scala
*/
package object mongoContext {
implicit val context = {
val context = new Context {
val name = "global"
override val typeHintStrategy = StringTypeHintStrategy(when = TypeHintFrequency.WhenNecessary, typeHint = "_t")
}
context.registerGlobalKeyOverride(remapThis = "id", toThisInstead = "_id")
context.registerClassLoader(Play.classloader)
context
}
}
import mongoContext._
/**
* class for storing hybridLInks
*/
case class HybridLink(
@Key("_id") id: ObjectId = new ObjectId,
val name: String,
val origin_id: String,
val origin_type: String,
val target_id: String,
val target_type: String)
/**
* the companion object for HybridLink that acts as a Dao
*/
object HybridLink extends ModelCompanion[HybridLink, ObjectId] {
def collection = MongoDBLayer.mongoDB("hybridlink")
val dao = new SalatDAO[HybridLink, ObjectId](collection) {}
def apply(name: String, origin: HybridEntity, target: HybridEntity): HybridLink =
{
HybridLink(null, name, origin.id, origin.getClass().getName(), target.id, target.getClass().getName())
}
/**
* finds all Objects that are pointing to a certain HybridEntity
*/
def findByReferenced(entity: HybridEntity): List[HybridEntity] = {
val it = find(MongoDBObject("target_id" -> entity.id, "target_type" -> entity.getClass().getName()))
val linkList = it.toList
for (link <- linkList)
yield this.getHybridEntitybyClassandId(link.origin_type, link.origin_id)
}
/**
* finds all Objects that are pointed at from a certain hybridEntity
*/
def findReferencing(entity: HybridEntity): List[HybridEntity] = {
val it = find(MongoDBObject("origin_id" -> entity.id, "origin_type" -> entity.getClass().getName()))
val linkList = it.toList
for (link <- linkList)
yield this.getHybridEntitybyClassandId(link.target_type, link.target_id)
}
/**
* finds a list of all outgoing Links
*/
def findReferencinglinks(entity: HybridEntity): List[HybridLink] = {
val it = find(MongoDBObject("origin_id" -> entity.id, "origin_type" -> entity.getClass().getName()))
val linkList = it.toList
return linkList
}
/**
* returns a list of all links that reference the specified HbyridEntity
*/
def findIncominglinks(entity: HybridEntity): List[HybridLink] = {
val it = find(MongoDBObject("target_id" -> entity.id, "target_type" -> entity.getClass().getName()))
val linkList = it.toList
return linkList
}
/**
* retrieves the corresponding HbyridEntity by id and type
*/
def getHybridEntitybyClassandId(cls: String, id: String): HybridEntity =
cls match {
case "model.hybrid.Space" => SpaceDao.findById(id)
case "model.hybrid.Page" => PageDao.findById(id)
case _ => throw new IllegalArgumentException("couldnt find corresponding dao for class " + cls)
}
/**
* helper method for removing all references that are pointing to a specified HybridEntity
*/
def removeAllReferences(entity: HybridEntity): Unit =
{
remove(MongoDBObject("target.id" -> entity.id, "target._typeHint" -> entity.getClass().getName()))
}
}
As you can see it uses a custom context to work with play's class loader.
However i cannot acess it in a Junit Test.
I get the error msg: "java.lang.NoClassDefFoundError: Could not initialize class backend.link.HybridLink$ at backend.core.search.LinkService$.linkObjects(LinkService.scala:31)"
Any ideas on how to tackle this problem would be appreciated-
That was pretty dumb of me, as I didn't even include the test method :/ (sorry for that)
I simply forgot to add:
@Test def testImport() {
running(FakeApplication()) {
//test code goes here
}
now everything works like a charm.
链接地址: http://www.djcxy.com/p/65688.html上一篇: 如何从上下文中查找重新映射的值
下一篇: 无法通过JUnit访问