微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

RestController 中的属性在 Junit 中不可访问且为 Null

如何解决RestController 中的属性在 Junit 中不可访问且为 Null

当我为 Rest Controller 类运行我的 junit 时,Rest 控制器中的属性抛出 Nullpointer 异常。下面是我的示例代码。当我运行 TestingControllerTest calass 的 testFileStatus() 方法时,我收到 Nullpointer 异常。请帮助解决问题。任何建议为什么我无法访问该属性,即使它在 test application.properties 中可用。

enter image description here

@RestController
@CrossOrigin(origins = "*",allowedHeaders = "*")
public class TestingController {
    @Value("${file.dir.status}")
    private String fileDirectoryStatus;

    @GetMapping(value="/filedetails")
    public String getFileDetails(.....){
        if(fileDirectoryStatus.equalsIgnoreCase("true")) {         //NullpointerException in Junit test
        //code to process file
        }
    }
}

src/main/resources/application.properties:

file.dir.status=false

src/test/resources/application.properties:

file.dir.status=false
@SpringBoottest
@TestPropertySource(
locations = "classpath:resources/application.properties")
public class TestingControllerTest{
    @InjectMocks
    private TestingController testingController; 

    @Test
    public void testFileStatus() {
    String status=testingController.getFileDetails(...);
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。