英文:
Why is my mockito code calling the real code?
问题
以下是翻译好的内容:
我正在使用Mockito在IntelliJ中编写Java测试。我试图使用Mockito来模拟一个API请求,但它似乎仍然在调用真实的代码,而不是使用从sendRequest返回的数据,导致我的测试失败。为什么会这样呢?
以下是代码:
public String sendRequest(){
return "1\n" +
"2\n" +
"3";
}
@Test
public void calculateWeatherCorrectly() throws IOException {
try {
WeatherCalculator weatherCalculator = mock(WeatherCalculator.class);
when(weatherCalculator.sendWeatherRequest("London", "01-01-2020")).thenReturn(sendRequest());
assertThat(midDayWeather("London", "12-01-2020"), equalTo(1.15));
} catch (IOException e) {
e.printStackTrace();
}
}
这是被测试方法的较小版本:
public static Double midDayWeather(String place, String date)
throws IOException {
WeatherCalculator weatherCalculator = new WeatherCalculator();
String l = weatherCalculator.sendWeatherRequest(place, date);
String[] result = l.split("\n");
return result.length;
}
英文:
I am using mockito to write a test in java in intellij. I am trying to use mockito to mock an api request,
but it still seems to be calling the real code and not using the data returned from sendRequest, causing my test to fail. Why is this?
Here is the code:
public String sendRequest(){
return "1\n" +
"2\n" +
"3";
}
@Test
public void calculateWeatherCorrectly() throws IOException {
try {
WeatherCalculator weatherCalculator = mock(WeatherCalculator.class);
when(weatherCalculator.sendWeatherRequest("London", "01-01-2020")).thenReturn(sendRequest());
assertThat(midDayWeather("London", "12-01-2020"), equalTo(1.15));
} catch (IOException e) {
e.printStackTrace();
}
}
Here is a smaller version of the method being tested:
public static Double midDayWeather(String place, String date)
throws IOException {
WeatherCalculator weatherCalculator = new WeatherCalculator();
String l = weatherCalculator.sendWeatherRequest(place, date);
String[] result = l.split("\n");
return result.length;
}
</details>
# 答案1
**得分**: 2
你已经使用`mock()`方法创建了一个模拟对象,并且正确地进行了设置。然而,你并没有在任何地方使用`WeatherCalculator`对象。你的`midDayWeather()`方法是静态的,不会使用你在测试方法中创建的模拟`WeatherCalculator`对象。事实上,你的`midDayWeather()`方法会创建它自己的`WeatherCalculator`对象(未经模拟)并使用它。
如果你的静态`midDayWeather()`方法应该与模拟方法一起工作,你需要将其作为参数传递。因此,你的方法应该类似于这样:
```java
public static Double midDayWeather(WeatherCalculator calculator,
String place, String date) throws IOException
{
calculator.sendWeatherRequest(...);
}
然后你可以将你的模拟对象作为参数传递进去。
英文:
You have created a mock object with the mock()
method and set it up correctly. However, you are not using the WeatherCalculator
object anywhere. Your method midDayWeather()
is static and will not use your mocked WeatherCalculator
object created in your test method. In fact, your midDayWeather()
method creates his own WeatherCalculator
object (which is not mocked) and use it instead.
If your static midDayWeather()
method should work with your mocked method you have to pass it as an argument. So you method should look something like this:
public static Double midDayWeather(WeatherCalculator calculator,
String place, String date) throws IOException
{
calculator.sendWeatherRequest(...);
}
Then you can pass your mocked object as an argument.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论