Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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 为RestTemplate注入TestRestTemplate_Java_Spring Mvc_Spring Boot - Fatal编程技术网

Java 为RestTemplate注入TestRestTemplate

Java 为RestTemplate注入TestRestTemplate,java,spring-mvc,spring-boot,Java,Spring Mvc,Spring Boot,我有一个组件是自动布线模板。我希望使用该组件进行测试,但希望使用TestRestTemplate。如我所见,TestRestTemplate和RestTemplate没有通过任何接口连接 RestTemplate实现RestOperations,但TestRestTemplate没有。有没有优雅的方法将TestRestTemplate注入RestTemplate,或者其他替代方法来实现它 我已经创建了自己版本的RestTemplate,以便在测试和生产中使用它 @Autowired privat

我有一个组件是自动布线模板。我希望使用该组件进行测试,但希望使用TestRestTemplate。如我所见,TestRestTemplate和RestTemplate没有通过任何接口连接

RestTemplate实现RestOperations,但TestRestTemplate没有。有没有优雅的方法将TestRestTemplate注入RestTemplate,或者其他替代方法来实现它

我已经创建了自己版本的RestTemplate,以便在测试和生产中使用它

@Autowired
private MyRestTemplate restTemplate;



你可以显示你的代码添加我的代码,我正在努力使其工作。但是我想使用testresttemplate而不是resttemplate
@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfiguration@ActiveProfiles(profiles=“Test”)公共类CustomTest{@Autowired private MyRestTemplate resttemplate;@Test public void Test(){//your custom testing}}
testresttemplate是为了在测试中使用,不在您正在测试的组件中。您试图实现什么?我创建了MyRestTemplate实用程序,它使用rest模板调用其他服务,但也在调用之前执行Validator.validate来验证请求。在顶部,它还检查响应状态和其他响应验证。这在prod中运行良好,但我无法在集成测试中用testresttemplate替换rest模板,因此MyRestTemplate使用测试rest模板而不是rest模板。您能否显示您的代码添加了我正在尝试使其工作的代码。但是我想使用testresttemplate而不是resttemplate
@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfiguration@ActiveProfiles(profiles=“Test”)公共类CustomTest{@Autowired private MyRestTemplate resttemplate;@Test public void Test(){//your custom testing}}
testresttemplate是为了在测试中使用,不在您正在测试的组件中。您试图实现什么?我创建了MyRestTemplate实用程序,它使用rest模板调用其他服务,但也在调用之前执行Validator.validate来验证请求。在顶部,它还检查响应状态和其他响应验证。这在prod中工作得很好,但在集成测试中我无法用testresttemplate替换rest模板,因此MyRestTemplate使用测试rest模板而不是rest模板。
public class MyRestTemplate {

    @Autowired
    private RestTemplate restTemplate;
}
@Configuration
@Profile("test")
public class MyConfig {

    @Bean
    public RestTemplate restTemplate() {
        return new TestRestTemplate().getRestTemplate();
    }
}