java - 使用 RestTemplate 进行 RESTful 服务测试

标签 java rest junit spring-boot

在我的应用程序中,我有很多 REST 服务。我已经为所有服务编写了测试:

org.springframework.web.client.RestTemplate

REST-服务调用,例如看起来像这样:

final String loginResponse = restTemplate.exchange("http://localhost:8080/api/v1/xy", HttpMethod.POST, httpEntity, String.class)
        .getBody();

然后我检查响应正文 - 一切正常。 缺点是,必须启动应用程序才能调用 REST 服务。

我现在的问题是如何在我的 JUnit-@Test 方法中做到这一点? 它是一个 Spring Boot 应用程序(带有嵌入式 tomcat)。

感谢您的帮助!

最佳答案

有一个很好的chapter关于文档中的这一点,我建议您仔细阅读它以充分了解您可以做什么。

我喜欢将 @IntegrationTest 与自定义配置一起使用,因为它会启动整个服务器并让您测试整个系统。如果您想用模拟替换系统的某些部分,您可以通过排除某些配置或 bean 并用您自己的配置或 bean 替换它们来实现。

这是一个小例子。我省略了 MessageService 接口(interface),因为从 IndexController 中可以明显看出它的作用,并且它是默认实现 - DefaultMessageService - 因为它不相关。

它的作用是启动整个应用程序,去掉 DefaultMessageService,但使用它自己的 MessageService。然后,它使用 RestTemplate 向测试用例中正在运行的应用程序发出真实的 HTTP 请求。

应用程序类:

IntegrationTestDemo.java:

@SpringBootApplication
public class IntegrationTestDemo {

    public static void main(String[] args) {
        SpringApplication.run(IntegrationTestDemo.class, args);
    }

}

IndexController.java:

@RestController
public class IndexController {

    @Autowired
    MessageService messageService;

    @RequestMapping("/")
    String getMessage() {
        return messageService.getMessage();
    }
}

测试类:

IntegrationTestDemoTest.java:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestConfig.class)
@WebIntegrationTest // This will start the server on a random port
public class IntegrationTestDemoTest {

    // This will hold the port number the server was started on
    @Value("${local.server.port}")
    int port;

    final RestTemplate template = new RestTemplate();

    @Test
    public void testGetMessage() {
        String message = template.getForObject("http://localhost:" + port + "/", String.class);

        Assert.assertEquals("This is a test message", message);
    }
}

TestConfig.java:

@SpringBootApplication
@ComponentScan(
    excludeFilters = {
        // Exclude the default message service
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = DefaultMessageService.class),
        // Exclude the default boot application or it's
        // @ComponentScan will pull in the default message service
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = IntegrationTestDemo.class)
    }
)
public class TestConfig {

    @Bean
    // Define our own test message service
    MessageService mockMessageService() {
        return new MessageService() {
            @Override
            public String getMessage() {
                return "This is a test message";
            }
        };
    }
}

关于java - 使用 RestTemplate 进行 RESTful 服务测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57547913/

相关文章:

java - 如何测试运行速度比瞬时慢的方法?

java - 依赖功能 JUnit 测试不能共享静态字段

web-services - 用于测试RESTful Web服务的工具

java - 如何使用 .stream() 将存储库中的数据按字母顺序排序

java - 在 EntityListener 中插入导致 Hibernate 中的 ConcurrentModificationException

java - RayCastingSupport.intersectRayWithTerrain 始终返回 null

javascript - express 中可变数量的路线参数?

rest - 如果 Web Service Location 不断变化,可以使用哪种 Architecture

junit - 如何仅对具有自定义注释的测试运行junit?

java - 如何更改父类(super class)方法的参数?