2013-04-30 51 views
0

我想在測試用例中調用隱式函數,但似乎Eclipse的UI不能識別它,我得到編譯錯誤,使用Eclipse - 構建ID:2.1-M2-20121018-1623-Typesafe Eclipse SDK版本: 3.7.2如何在junit測試用例中調用scala的隱式函數?

我錯過了什麼嗎?在.brokeragePricing

代碼

package com.scala.dsl 

object TradeDSL { 
    abstract class Instrument(name: String) { def instrumentName: String } 
    case class Stock(name: String) extends Instrument(name) { 
     override val instrumentName = "EQUITY"; 
    } 
    case class Bond(name: String) extends Instrument(name) { 
     override val instrumentName = "BOND"; 
    } 

    abstract class TransactionType { def value: String} 
    case class Buy() extends TransactionType { 
     override val value = "BUY" 
    } 

    case class Sell() extends TransactionType { 
     override val value = "SELL" 
    } 

    class PricingStrategy(order: Order) { 
     def defaultPricing(): Int = order.quantity * order.price 
     def brokeragePricing(): Int = order.quantity * order.price + 100 

    } 

    implicit def orderPricing(order: Order) = new PricingStrategy(order); 

    case class Order(price: Int = 0, instrument: Instrument = null, quantity: Int = 0, totalValue: Int = 0,trn: TransactionType = null, account: String = null) { 
     def maxUnitPrice(p: Int) = copy(price = p) 
     def to(i: Tuple2[Instrument, Int]) = copy(instrument = i._1, quantity = i._2) 
     def buy(qi: Tuple2[Int, Instrument]) = copy(instrument = qi._2, quantity = qi._1, trn = Buy()) 
     def sell(qi: Tuple2[Int, Instrument]) = copy(instrument = qi._2, quantity = qi._1, trn = Sell()) 
     def using(pricing: (Int, Int) => Int) = { 
      copy(totalValue = pricing(quantity, price)) 
     } 
     def forAccount(a: String)(implicit pricing: (Int,Int) => Int) = { 
     copy(account = a, totalValue = pricing(quantity,price)) 
     } 

    } 
} 

測試用例

import org.scalatest.FunSuite 
import org.junit.runner.RunWith 
import org.scalatest.junit.JUnitRunner 
import com.scala.dsl._ 
import com.scala.dsl.TradeDSL.Bond 
import com.scala.dsl.TradeDSL.Stock 
import com.scala.dsl.TradeDSL.Buy 
import com.scala.dsl.TradeDSL.Sell 
import com.scala.dsl.TradeDSL.Order 

@RunWith(classOf[JUnitRunner]) 
class TestTradeDSL extends FunSuite { 


    test("Order") { 
    val order1 = new Order() 
     .buy(10, Stock("GOLD")) 
     .maxUnitPrice(25) 
     .brokeragePricing 
    } 

} 

獲取編譯錯誤,我試圖清理項目&重啓食,但一切都是徒勞。

+0

不給一個明確的返回類型maxUnitPrice幫助? – Dan 2013-04-30 17:30:06

回答

0

問題是,您正在逐一導入TradeDSL對象。您可以通過導入對象的所有元素修復:

import com.scala.dsl.TradeDSL._ 

或導入缺少的元素:

import com.scala.dsl.TradeDSL.orderPricing