英文:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论