【问题标题】:ScalaTest plugin in eclipse not finding my classeclipse中的ScalaTest插件找不到我的课
【发布时间】:2016-08-04 06:38:36
【问题描述】:

我正在尝试使用 ScalaTest 来测试 Java 项目。我已经编写了测试并且它可以工作(我知道是因为我已经在 IntelliJ 中成功运行了它们),但是我还没有让它在 eclipse 中工作。

这是我的代码(测试的内部应该不太重要):

@ContextConfiguration(
  classes = Array(classOf[OrderApplicationSpecContext]),
  loader = classOf[SpringApplicationContextLoader])
class PlacingOrderSpec extends path.FunSpec with org.scalatest.Matchers {

  @Autowired val orderApplication: OrderApplication = null
  @Autowired val customerRepository: CustomerRepository = null
  @Autowired val orderRepository: OrderRepository = null
  @Autowired val creditCardService: CreditCardService = null

  new TestContextManager(this.getClass).prepareTestInstance(this)

  describe("Placing an Order") {

    describe("With an existing customer") {

      val customer = new Customer()
      customerRepository.save(customer)

      it("should return a valid order ID") {
        val orderId = orderApplication.placeOrder(123L, customer.getTid, "123")
        orderId should not be null
      }

      describe("When the credit card is not maxed out and not expired") {

        Mockito.when(creditCardService.isMaxedOut("123", 1.23)).thenReturn(false)
        Mockito.when(creditCardService.expirationDate("123")).thenReturn(LocalDate.MAX)

        it("Should create an order with status <IN PROGRESS>") {
          val orderId = orderApplication.placeOrder(123L, customer.getTid, "123")
          val orderStatus = orderApplication.getOrderStatus(orderId)

          orderStatus should be("IN PROGRESS")
        }

        it("Should create an order accessible from the customer") {
          val orderId = orderApplication.placeOrder(123L, customer.getTid, "123")
          val orders = orderApplication.findOrdersForCustomer(customer.getTid)

          orders should not be empty
          Inspectors.forExactly(1, orders) {
            _.getTid should be (orderId)
          }
        }

      }

      describe("When the credit card is maxed out") {

        Mockito.when(creditCardService.isMaxedOut("123", 1.23)).thenReturn(true)

        it("Should create an order with status <PAYMENT FAILED>") {
          val orderId = orderApplication.placeOrder(123L, customer.getTid, "123")
          val orderStatus = orderApplication.getOrderStatus(orderId)

          orderStatus should be("PAYMENT FAILED")
        }

        it("Should create an order not accessible from the customer") {
          val orderId = orderApplication.placeOrder(123L, customer.getTid, "123")
          val orders = orderApplication.findOrdersForCustomer(customer.getTid)

          Inspectors.forAll(orders) {
            _.getTid should not be(orderId)
          }
        }

      }

      describe("When the credit card is expired") {
        Mockito.when(creditCardService.isMaxedOut("123", 1.23)).thenReturn(false)
        Mockito.when(creditCardService.expirationDate("123")).thenReturn(LocalDate.now().plusMonths(1))

        it("Should create an order with status <PAYMENT FAILED>") {
          val orderId = orderApplication.placeOrder(123L, customer.getTid, "123")
          val orderStatus = orderApplication.getOrderStatus(orderId)

          orderStatus should be("PAYMENT FAILED")
        }

        it("Should create an order not accessible from the customer") {
          val orderId = orderApplication.placeOrder(123L, customer.getTid, "123")
          val orders = orderApplication.findOrdersForCustomer(customer.getTid)

          Inspectors.forAll(orders) {
            _.getTid should not be(orderId)
          }
        }
      }

      customerRepository.deleteAll()

    }

  }

}

我试过的:

  • 菜单Run &gt; Run As...:没有适用的选项。
  • 创建一个新的“ScalaTest”配置。但是当我想选择我的“Suite Class”时,找不到任何课程。
  • 将@RunWith(classOf[JUnitRunner]) 放在我的班级顶部。然后我可以做Menu Run &gt; Run As... &gt; Junit Test。但我收到一条错误消息,提示“未找到 JUnit 测试”。

但是,我可以右键单击我的项目,创建一个 Scala 解释器并键入 (new PlacingOrderSpec()).execute(),这将起作用(但不会使用 IDE 的 junit 视图,并强制我在每次更改后手动重新编译。

我错过了什么?

【问题讨论】:

    标签: java eclipse scala junit4 scalatest


    【解决方案1】:

    我终于成功地用 JUnit 运行了我的测试。

    问题是我的文件顶部缺少package 语句。有了它和 @RunWith 注释,我就可以使用 JUnit 运行测试了。但是,它仍然不能与 ScalaTest 插件一起使用,但这对我来说没问题。

    ScalaTest 插件问题欢迎其他答案。

    【讨论】:

      猜你喜欢
      • 2012-12-27
      • 2014-10-07
      • 1970-01-01
      • 2016-12-02
      • 1970-01-01
      • 2012-09-13
      • 1970-01-01
      • 1970-01-01
      • 2015-07-27
      相关资源
      最近更新 更多