被模拟的函数未被调用,而是使用了真实函数。

huangapple go评论73阅读模式
英文:

Mocked function is not called, instead is used real function

问题

我开始学习模拟我希望当调用时测试将失败仅用于学习目的)。

MathUtils类

    public class MathUtils {

        public int addWithHelper(int a, int b) {
            MathUtilsHelper mathUtilsHelper = new MathUtilsHelper();
            return mathUtilsHelper.addNumbers(a, b);
        }
    }
    
而我的MathUtilsHelper类

    public class MathUtilsHelper {

        int addNumbers(int a, int b) {
            return a + b;
        }
    }

MathUtilsTest类

    @Test
    void itShouldAddNumberFromHelper() {

        MathUtilsHelper mathUtilsHelperMocked = Mockito.mock(MathUtilsHelper.class);
        when(mathUtilsHelperMocked.addNumbers(5, 3)).thenReturn(999); // 这个不起作用!!!!

        int add = mathUtils.addWithHelper(5, 3);
        assertEquals(8, add); // 应该抛出错误
    }

感谢任何帮助
英文:

I started to study mocking. I want that test will fail when is called (only for study purpose).

MathUtils class:

public class MathUtils {

  public int addWithHelper(int a, int b) {
        MathUtilsHelper mathUtilsHelper = new MathUtilsHelper();
        return mathUtilsHelper.addNumbers(a,b);
    }
}

And my MathUtilsHelper:

public class MathUtilsHelper {

     int addNumbers(int a, int b) {
         return a + b;
     }
}

Class MathUtilsTest

@Test
void itShouldAddNumberFromHelper() {

     MathUtilsHelper mathUtilsHelperMocked = Mockito.mock(MathUtilsHelper.class);
     when(mathUtilsHelperMocked.addNumbers(5,3)).thenReturn(999); // this doesn't works !!!!!!

     int add = mathUtils.add(5, 3);
     assertEquals(8, add); // should throw error

}

Thank you for any help!

答案1

得分: 1

你的工具类每次都在创建一个新的助手实例,因此永远不会使用模拟。

老实说,我不太确定你为什么需要这个工具类,但是如果你想要更容易进行测试,可以通过在构造函数中传入助手实例来改变它,而不是在工具类中实例化助手。换句话说,这就是依赖注入。

这样,你就可以创建模拟,然后通过传入模拟来创建工具类的实例。

英文:

Your util class is creating a new instance of your helper each time, so will never use the mock.

I'm not sure why you need the util class to be honest, but if you want to make it easier to test, change it so that an instance of the helper is passed in on the constructor rather than instantiating it in the util class. Dependency injection in other words.

That way you can create the mock, and create an instance of the util class by passing in the mock.

答案2

得分: 1

MathUtils```类中没有模拟对象您可以按照以下方式对```MathUtils```类进行操作

```java
public class MathUtils {
  public MathUtilsHelper mathUtilsHelper;

  public MathUtils(MathUtilsHelper mathUtilsHelper ){
     this.mathUtilsHelper=mathUtilsHelper;
  }

  public int addWithHelper(int a, int b) {
     return mathUtilsHelper.addNumbers(a,b);
  }
}

在初始化测试时,请尝试以下操作:

@Test
void itShouldAddNumberFromHelper() {

   MathUtilsHelper mathUtilsHelperMocked = Mockito.mock(MathUtilsHelper.class);
   when(mathUtilsHelperMocked.addNumbers(5,3)).thenReturn(999);
   mathUtils = new MathUtils(mathUtilsHelperMocked);

   int add = mathUtils.addWithHelper(5, 3);
   assertEquals(8, add);

}
英文:

MathUtils doesn't have the mocked object in it, you do as follows for MathUtils class:

public class MathUtils {
  public MathUtilsHelper mathUtilsHelper;

  public MathUtils(MathUtilsHelper mathUtilsHelper ){
     this.mathUtilsHelper=mathUtilsHelper;
  }

  public int addWithHelper(int a, int b) {
     return mathUtilsHelper.addNumbers(a,b);
  }
}

And when initializing your test try this:

@Test
void itShouldAddNumberFromHelper() {

   MathUtilsHelper mathUtilsHelperMocked = Mockito.mock(MathUtilsHelper.class);
   when(mathUtilsHelperMocked.addNumbers(5,3)).thenReturn(999);
   mathUtils= new MathUtils(mathUtilsHelperMocked);

   int add = mathUtils.addWithHelper(5, 3);
   assertEquals(8, add);

}

huangapple
  • 本文由 发表于 2020年9月7日 16:46:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63774237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定