Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.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/spring-boot/5.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
Java 如何使用Spring Boot将InputStreamReader作为参数注入BufferedReader?_Java_Spring Boot_Unit Testing_Dependency Injection_Mockito - Fatal编程技术网

Java 如何使用Spring Boot将InputStreamReader作为参数注入BufferedReader?

Java 如何使用Spring Boot将InputStreamReader作为参数注入BufferedReader?,java,spring-boot,unit-testing,dependency-injection,mockito,Java,Spring Boot,Unit Testing,Dependency Injection,Mockito,我的问题和你的一样。但是,我的BufferedReader将InputStreamReader作为参数。我的代码如下所示: URL url = new URL("http://localhost:8080/endpoint"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //Line of importance: BufferedReader br = new BufferedReader

我的问题和你的一样。但是,我的BufferedReader将InputStreamReader作为参数。我的代码如下所示:

URL url = new URL("http://localhost:8080/endpoint");

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

//Line of importance:
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
我如何注入它,以便模拟它进行单元测试?提前谢谢

编辑:添加测试代码(我认为它应该如何工作):

添加bean(?):

使用模拟实例模拟所有依赖项:

@Mock
private HttpURLConnection httpURLConnection;

@Mock
private BufferedReader bufferedReader;

@Mock
private InputStream inputStream;

    when(URLhandlerClass.getPath(any(String.class))).thenReturn("http://localhost:8080/endpoint);
    when(bufferedReader.readLine()).thenReturn("firstLine", "secondLine", "thirdLine");
    when(httpURLConnection.getInputStream()).thenReturn(inputStream);

错误:FileNotFoundException和Connect:连接被拒绝

您到底想模拟什么? 我在这里看到的最简单的方法是伪造整个方法,例如,在测试代码时将所有内容包装在一个服务中并模拟整个方法

e、 g

这很容易被伪造。
通常,当您遇到一个问题时,您会问自己“wtf I mock this?”解决方案是服务;)

你到底想嘲笑什么? 我在这里看到的最简单的方法是伪造整个方法,例如,在测试代码时将所有内容包装在一个服务中并模拟整个方法

e、 g

这很容易被伪造。
通常,当您遇到一个问题时,您会问自己“wtf I mock this?”解决方案是服务;)

谢谢你的回答!然而,我仍然不明白如何模拟测试它。我知道这也会让你感到困惑,因为我不知道如何测试它,但我认为我需要用参数模拟BufferedReader。我会把我的想法添加到OPS中,谢谢你的回答!然而,我仍然不明白如何模拟测试它。我知道这也会让你感到困惑,因为我不知道如何测试它,但我认为我需要用参数模拟BufferedReader。我将把我的想法添加到OPA中,当只对模拟有效时,您没有任何模拟,因此它无法工作。我不会费心对这四行代码进行单元测试——这很困难,而且基本上是测试框架代码,而框架代码很少有用。更令人感兴趣的是对内容执行某些操作的代码,例如,您没有显示的读取BufferedReader的代码。@ChristianSauer我用模拟实例更新了代码。感谢您的贡献,您是对的,它测试框架代码。然而,我的端点只有一个入口(一个公共方法),为了测试所有其余的代码,我必须模拟上面的框架代码,以便它可以继续执行“内容”代码。只有一个入口是正常的。但您的问题是,您的代码是一个整体。使用服务或类似的东西将代码划分为可测试组件。您的问题不是框架代码本身,而是您的代码不是为可测试性而设计的。如果您的控制器上有一个/post方法,那么您的代码应该是这样的:BufferedReader data=dataService.GetBufferedReader(…)dataProcessor.processData(data)@ChristianSauer好的,这很有意义。谢谢你带我看了一下(=)当只在模拟上工作时,你没有任何模拟,因此它不能工作。我不会费心对这四行代码进行单元测试——这很困难,而且基本上是测试框架代码,而框架代码很少有用。更令人感兴趣的是对内容执行某些操作的代码,例如,您没有显示的读取BufferedReader的代码。@ChristianSauer我用模拟实例更新了代码。感谢您的贡献,您是对的,它测试框架代码。然而,我的端点只有一个入口(一个公共方法),为了测试所有其余的代码,我必须模拟上面的框架代码,以便它可以继续执行“内容”代码。只有一个入口是正常的。但您的问题是,您的代码是一个整体。使用服务或类似的东西将代码划分为可测试组件。您的问题不是框架代码本身,而是您的代码不是为可测试性而设计的。如果您的控制器上有一个/post方法,那么您的代码应该是这样的:BufferedReader data=dataService.GetBufferedReader(…)dataProcessor.processData(data)@ChristianSauer好的,这很有意义。谢谢你带我走过这段路=)
@Mock
private HttpURLConnection httpURLConnection;

@Mock
private BufferedReader bufferedReader;

@Mock
private InputStream inputStream;

    when(URLhandlerClass.getPath(any(String.class))).thenReturn("http://localhost:8080/endpoint);
    when(bufferedReader.readLine()).thenReturn("firstLine", "secondLine", "thirdLine");
    when(httpURLConnection.getInputStream()).thenReturn(inputStream);
@Service
public class DataReader {
    public BufferedReader ReadDataFrom(string url){
        URL url = new URL("http://localhost:8080/endpoint");

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        //Line of importance:
        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
    }
}