eclipse 中的 ScalaTest 插件找不到我的 class

ScalaTest plugin in eclipse not finding my class

我正在尝试使用 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()

    }

  }

}

我尝试过的:

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

我错过了什么?

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

问题是我的文件顶部缺少 package 语句。通过它和 @RunWith 注释,我能够 运行 使用 JUnit 进行测试。然而,它仍然不适用于 ScalaTest 插件,但这对我来说很好。

欢迎针对 ScalaTest 插件问题提供其他答案。