Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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
Spring boot @JUnit5中的MOCK在构造函数中使用@Value时返回NULL_Spring Boot_Unit Testing_Mockito_Junit5_Fileinputstream - Fatal编程技术网

Spring boot @JUnit5中的MOCK在构造函数中使用@Value时返回NULL

Spring boot @JUnit5中的MOCK在构造函数中使用@Value时返回NULL,spring-boot,unit-testing,mockito,junit5,fileinputstream,Spring Boot,Unit Testing,Mockito,Junit5,Fileinputstream,我正在为一个在多个地方使用文件inpurstream的项目编写测试用例。由于PowerMock和Junit5的问题,我最终使用了一个带@Value的构造函数将文件路径传递给类。示例代码将显示 现在,当我使用@Value的构造函数时。文件inputStream的测试用例工作得很好。但是@MOck for任何其他类返回NULL。 我对构造函数进行注释,@Mock开始工作 以前有人遇到过这种情况吗 待测类别: @Component public class test { TestUtils

我正在为一个在多个地方使用文件inpurstream的项目编写测试用例。由于PowerMock和Junit5的问题,我最终使用了一个带@Value的构造函数将文件路径传递给类。示例代码将显示

现在,当我使用@Value的构造函数时。文件inputStream的测试用例工作得很好。但是@MOck for任何其他类返回NULL。 我对构造函数进行注释,@Mock开始工作

以前有人遇到过这种情况吗

待测类别:

@Component
public class test {


    TestUtils testUtil;

    String nodeConfigFilePath;
    String genConfigFilePath;

    public test (@Value("${node.config.path}") String nodeConfigFilePath, 
            @Value("${genConfig.path}", @Autowired TestUtils testUtil) String genConfigFilePath) {
        this.nodeConfigFilePath=nodeConfigFilePath;
        this.genConfigFilePath= genConfigFilePath;
        this.testUtil = testUtil;
    }



    public boolean filterConfig(JSONObject Json) {
                if(testUtil.iterateJsonForPatternMatch(Json))
                    return true;
                else
                    return false;
            }
        public void loadConfigFromJson() {
        ObjectMapper mapper = new ObjectMapper();
        TypeReference<Map<String, String>> typeRef = new TypeReference<Map<String, String>>() {
        };
        try {
            InputStream inputStream = new FileInputStream(nodeConfigFilePath);
            Map<String, String> nodeConfig = new HashMap<String, String>();
            nodeConfig = mapper.readValue(inputStream,typeRef);
            <do something>
            }
        }
        catch(Exception e) {

        }
    }


}
仅供参考:为了演示,我更改了方法名称并创建了一个虚拟类

因此,如果现在我删除@Value构造函数,它将开始工作,并且testUtil没有空指针。但是testloadConfigFromJson_success()开始失败。反之亦然。有人能解释为什么会这样吗


更新:将autoiring移动到构造函数,并通过测试类中的构造函数传递TestUtil实例。这似乎奏效了。我不确定它是否正确

我认为您的
@Mock
/
@InjectMocks
变量是错误的。他们应该走另一条路吗。。。。因为TestUtils应该是模拟的,并且应该在测试中注入,或者为自己做很多事情。1.尊重Java命名约定。2.停止使用字段注入:所有需要注入的内容,包括TestUtils依赖项,都通过构造函数注入。3.您不再需要Mock、injectmock和MockitoExtension:您只需要创建一个Mock TestUtil和两个字符串,并在测试中使用这3个参数调用构造函数。@pL4Gu33 RunWith用于JUnit 4,Mock和injectmock位于正确的变量上。是的,对不起,您是对的。。。。第一次喝咖啡之前不要发帖:D@JBNizet:谢谢你的回复。1) 这些是供参考的伪类,而不是实际的代码问题。对于第三点,您的意思是我创建了一个构造函数并将TestUtil从那里注入到测试中,因为我使用Autowired在测试类中需要的地方注入bean。
@ExtendWith(MockitoExtension.class)
public class test {

    @InjectMocks
    test test;

    @Mock
    TestUtils testUtil;

    @Test
    public void testFilterConfig_success() throws Exception{
        JSONObject jsonObj = new JSONObject();
        when(testUtil.iterateJsonForPatternMatch(jsonObj)).thenReturn(true);
        assertEquals(true, test.filterConfig(jsonObj));

    }
    @Test
       public void testloadConfigFromJson_success() throws Exception{
         test = new NotificationFilter(nodeConfigFile.getAbsolutePath(),genConfigJsonFile.getAbsolutePath(), testUtil);
        test.loadConfigFromJson();
    }

}