eclipse中的ScalaTest插件没有找到我的课程

时间:2016-04-13 09:14:48

标签: java eclipse scala junit4 scalatest

我正在尝试使用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 > Run As...:没有适用的选项。
  • 创建新的“ScalaTest”配置。但是当我想选择我的“套件类”时,找不到任何课程。
  • 将@RunWith(classOf [JUnitRunner])放在我的班级之上。然后我可以做Menu Run > Run As... > Junit Test。但我得到一个错误,说“没有发现JUnit测试”。

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

我错过了什么?

1 个答案:

答案 0 :(得分:0)

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

问题是我文件顶部缺少package语句。使用它和@RunWith注释,我可以使用JUnit运行测试。但是,它仍然无法与ScalaTest插件一起使用,但这对我的情况来说很好。

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