为什么我的Mockito代码调用了实际的代码?

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

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.

huangapple
  • 本文由 发表于 2020年4月4日 02:25:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/61018196.html
匿名

发表评论

匿名网友

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

确定