简体   繁体   中英

How to call implicit functions of scala inside junit test cases?

I am trying to call a implicit function inside a test case but seems eclipse UI is not able to recognize it, I am getting compilation error, using Eclipse - Build id: 2.1-M2-20121018-1623-Typesafe Eclipse SDK Version: 3.7.2

Am I missing something ?

Code

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))
      }

    }
}

Test Case

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
  }

}

Getting compilation error at .brokeragePricing I tried cleaning the project & restarting eclipse but all in vain.

The problem is that you are importing piece by piece of the TradeDSL object. You can fix it by importing all elements of the object:

import com.scala.dsl.TradeDSL._

Or by importing the missing element:

import com.scala.dsl.TradeDSL.orderPricing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM