Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 无法使用mockito/powermock模拟singleton类_Scala_Unit Testing_Junit_Mocking - Fatal编程技术网

Scala 无法使用mockito/powermock模拟singleton类

Scala 无法使用mockito/powermock模拟singleton类,scala,unit-testing,junit,mocking,Scala,Unit Testing,Junit,Mocking,错误是: @RunWith(classOf[PowerMockRunner]) @PrepareForTest(Array(classOf[mySingleTon])) class myTest { @Test def test(): Unit = { val mockInstance = org.mockito.Mockito.mock[mySingleTon] //val mockInstance = org.powermock.api.mockito.PowerMoc

错误是:

@RunWith(classOf[PowerMockRunner])
@PrepareForTest(Array(classOf[mySingleTon]))
class myTest  {

  @Test
  def test(): Unit = {
    val mockInstance = org.mockito.Mockito.mock[mySingleTon]  //val mockInstance = org.powermock.api.mockito.PowerMockito.mock(classOf[mySingleTon])
    Whitebox.setInternalState(classOf[mySingleTon], "INSTANCE":String, mockInstance:Any)
    when(mockInstance.getAerospikeCommand(nameCapture.capture())).thenReturn(mockedFunc)
我还尝试了以下建议:

但随后我开始出现以下错误:

@RunWith(classOf[PowerMockRunner])
@PrepareForTest(Array(classOf[mySingleTon]))
class myTest  {

  @Test
  def test(): Unit = {
     import org.powermock.api.mockito.PowerMockito
     PowerMockito.mockStatic(classOf[mySingleTon])
     BDDMockito.given(mySingleTon.INSTANCE.getCommand(nameCapture.capture())).willReturn(mockedCommand)

不能模拟枚举。更重要的是,你不应该这样做。mock是为了剔除业务逻辑。@Dima在我的例子中,有一些DB调用正在发生,我想对其进行模拟。应该有一些方法,我提到的链接显示了一些方法,但是无法复制这些方法。您无法复制是一种指示,这不是真正的“一种方法”:)从枚举调用db听起来真的。。。创新,你说这不是你的想法,你不能改变它,但你仍然可以控制它是否或如何被实际使用。。。一种选择是将代码更改为不直接使用枚举。制作一个包装类,向调用者公开一个API接口,它将把调用转发给你的不幸的枚举,然后模仿它。@Dima谢谢你的建议,我已经根据你的评论更新了使用它的代码。
@RunWith(classOf[PowerMockRunner])
@PrepareForTest(Array(classOf[mySingleTon]))
class myTest  {

  @Test
  def test(): Unit = {
    val mockInstance = org.mockito.Mockito.mock[mySingleTon]  //val mockInstance = org.powermock.api.mockito.PowerMockito.mock(classOf[mySingleTon])
    Whitebox.setInternalState(classOf[mySingleTon], "INSTANCE":String, mockInstance:Any)
    when(mockInstance.getAerospikeCommand(nameCapture.capture())).thenReturn(mockedFunc)
org.mockito.exceptions.base.MockitoException: 
Cannot mock/spy class com.my.cap.myModule.Singleton
Mockito cannot mock/spy following:
  - final classes
  - anonymous classes
  - primitive types

    at org.scalatest.mock.MockitoSugar$class.mock(MockitoSugar.scala:74)
@RunWith(classOf[PowerMockRunner])
@PrepareForTest(Array(classOf[mySingleTon]))
class myTest  {

  @Test
  def test(): Unit = {
     import org.powermock.api.mockito.PowerMockito
     PowerMockito.mockStatic(classOf[mySingleTon])
     BDDMockito.given(mySingleTon.INSTANCE.getCommand(nameCapture.capture())).willReturn(mockedCommand)
java.lang.IllegalArgumentException: Cannot subclass final class class com.cap.myModule.mySingleTon

    at org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:447)
    at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217)
    at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378)
    at org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318)