5
我做了一個簡單的類,自然地分成兩個部分,所以我重構爲Scala和使用的Mockito性狀
class Refactored extends PartOne with PartTwo
然後單元測試啓動失敗。
下面是重新創建問題的嘗試。所有三個示例的功能都是相同的,但第三個測試會失敗,並顯示NullPointerException。什麼是使用導致mockito問題的特質?
編輯: Mockito是Scala的最佳選擇嗎?我使用了錯誤的工具嗎?
import org.scalatest.junit.JUnitSuite
import org.scalatest.mock.MockitoSugar
import org.mockito.Mockito.when
import org.junit.Test
import org.junit.Before
class A(val b:B)
class B(val c:Int)
class First(){
def getSomething(a:A) = a.b.c
}
class Second_A extends Second_B
class Second_B{
def getSomething(a:A) = a.b.c
}
class Third_A extends Third_B
trait Third_B{
// Will get a NullPointerException here
// since a.b will be null
def getSomething(a:A) = a.b.c
}
class Mocking extends JUnitSuite with MockitoSugar{
var mockA:A = _
@Before def setup { mockA = mock[A] }
@Test def first_PASSES {
val mockFirst = mock[First]
when(mockFirst.getSomething(mockA)).thenReturn(3)
assert(3 === mockFirst.getSomething(mockA))
}
@Test def second_PASSES {
val mockSecond = mock[Second_A]
when(mockSecond.getSomething(mockA)).thenReturn(3)
assert(3 === mockSecond.getSomething(mockA))
}
@Test def third_FAILS {
val mockThird = mock[Third_A]
//NullPointerException inside here (see above in Third_B)
when(mockThird.getSomething(mockA)).thenReturn(3)
assert(3 === mockThird.getSomething(mockA))
}
}
我認爲直接嘲笑它,但它似乎比測試整體班乾淨。那麼,現在已經夠好了。 – Pengin 2011-06-06 21:34:14