无法通过JUnit访问
我有以下课程:
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()))
}
}
正如你所看到的,它使用一个自定义的上下文来处理play的类加载器。
但是我无法在Junit测试中使用它。
我收到错误消息:“java.lang.NoClassDefFoundError:无法初始化backend.core.search.LinkService $ .linkObjects(LinkService.scala:31)”类的backend.link.HybridLink $“
如何解决这个问题的任何想法,将不胜感激 -
这对我来说很愚蠢,因为我甚至没有包括测试方法:/(抱歉)
我只是忘了补充一下:
@Test def testImport() {
running(FakeApplication()) {
//test code goes here
}
现在一切都像魅力一样。
链接地址: http://www.djcxy.com/p/65687.html下一篇: Still can't run multiple tests against play FakeApp with Salat / Casbah