【发布时间】: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 > Run As...:没有适用的选项。 - 创建一个新的“ScalaTest”配置。但是当我想选择我的“Suite Class”时,找不到任何课程。
- 将@RunWith(classOf[JUnitRunner]) 放在我的班级顶部。然后我可以做
Menu Run > Run As... > Junit Test。但我收到一条错误消息,提示“未找到 JUnit 测试”。
但是,我可以右键单击我的项目,创建一个 Scala 解释器并键入 (new PlacingOrderSpec()).execute(),这将起作用(但不会使用 IDE 的 junit 视图,并强制我在每次更改后手动重新编译。
我错过了什么?
【问题讨论】:
标签: java eclipse scala junit4 scalatest