簡體   English   中英

測試Spring Boot REST資源問題

[英]Testing Spring boot REST resource issues

Spring Boot提供了一個微服務。 它由Jetty,Jersey,Jackson和Liquibase組成。 該服務的任務之一是通過REST接收一些數據並返回響應。 此操作將通過下一個單元:

  • MyResource具有JAX-RS批注的@Component REST,它接收數據並要求@Autowired MyService進行響應。
  • MyService@Component服務和@Autowired MyResource要求其響應。
  • MyResource ,簡單的@JpaRepository接口

該應用程序運行良好,但是現在我需要為每個模塊添加一些測試。 通常,我使用Mockito來測試單元,因此我使用模擬的MyRepository(Mockito @Mock批注+ when()方法)來測試我的服務。 我想以相同的方式測試MyResource.java。

我嘗試使用TestRestTemplate方式對spring-boot-starter-test進行測試,而我的測試類如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebIntegrationTest(randomPort = true)
public class MyResourceTest {
  @Value("${local.server.port}")
  private int port;

  private String getBaseUrl() {
    return "http://localhost:" + port;
  }

  @Test
  public void test() {
    final TestRestTemplate restTemplate = new TestRestTemplate();
    assertEquals(restTemplate.postForEntity(getBaseUrl() + "/test", null, ResponseObject.class).getBody(), new ResponseObject());
  }
}

而且有兩個問題。 首先-當我的測試正在運行時,它們會運行整個spring應用程序,因此我的liquibase腳本正在嘗試查找數據庫,這是一個很長的過程。 第二-我無法用Mockito代理替換MyService類。

我試圖找到一些有關測試Spring Boot REST應用程序最佳實踐的手冊,並且發現了基於MockMvc的方法,但是看起來好像沒有運行服務器來運行測試。 您能否分享在春季啟動時測試REST資源的方式?

MockMvc是您的問題的首選解決方案。 它運行spring boot應用程序,但是“嘲笑”請求,因此它實際上並沒有運行在http上,而是表現為如此。 您可以使用@Autowired將應用程序的所有bean注入到測試類中,以便在那里模擬spring bean。

我對6類配置/支持進行了所有測試。

AbstractTest配置測試核心

@ActiveProfiles(resolver = TestActiveProfilesResolver.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@IntegrationTest
@SpringApplicationConfiguration(classes = Application.class)
public abstract class AbstractTest {
   ...
}

AbstractRepositoryTest以通過jdbc + jdbi測試我的存儲庫

@Transactional
public abstract class AbstractRepositoryTest<R> extends AbstractTest implements Repositories {

    @Inject
    private ObjectMapper mapper;

    @Inject
    protected Repositories repositories;

    private final Class<R> repositoryType;

    ...
}

AbstractServiceTest來測試我的服務,此類包含了我的服務測試的核心

public abstract class AbstractServiceTest {
    ...
}

AbstractIntegrationTest包含我的集成測試的核心,用於測試控制器的utils方法等。

public abstract class AbstractIntegrationTest extends AbstractTest {
    ...
}

應用程序AbstractTest提供了運行測試的上下文,該類與我用來運行Spring Boot應用程序的類相同

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
    public static void main(final String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
    ...
}

最后是TestActiveProfilesResolver ,它提供用於匹配application-test.properties的配置文件,這是必要的,因為JIRA上存在未解決的問題, 此處

public class TestActiveProfilesResolver implements ActiveProfilesResolver {

    @Override
    public String[] resolve(final Class<?> testClass) {
        final String activeProfile = System.getProperty("spring.profiles.active");
        return new String[] {activeProfile == null ? "test" : activeProfile};
    }

}

有時我的測試擴展了AbstractRepositoryTestAbstractIntegrationTestsAbstractServiceTest ,這取決於我想要的東西。 這種配置解決了我所有的問題,以測試服務控制器等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM