在REST端点中为JUnit测试注入服务的测试实现

时间:2019-01-30 18:34:15

标签: java rest unit-testing spring-boot jersey

我正在开发REST应用,并且想要执行JUnit测试,将服务的测试实现注入我的资源(端点)

DocumentProviderApplication

package com.coriaedu.documentprovider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DocumentProviderApplication {

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

我正在使用Spring Boot和Jersey 2.27。我有一个终结点 DocumentProviderResource ,它依赖于注入的 DocumentManager 实现。

DocumentProviderResource

package com.coriaedu.documentprovider.service;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Component;

import com.coriaedu.documentprovider.interfaces.DocumentManager;

@Component
@Path("/documents")
public class DocumentProviderResource {

    @Inject
    DocumentManager manager;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public Response getDocumentLocation(@QueryParam("id") String docId) {
        return Response.ok(manager.getDocumentLocation(docId)).build();
    }
}

我创建了一个 ProductionConfiguration 类,以配置要在生产期间注入的bean。此 ExternalDocumentManager 实现可以用于生产,但不能用于开发。

package com.coriaedu.documentprovider.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.coriaedu.documentprovider.managers.ExternalDocumentManager;

@Configuration
public class ProductionConfiguration {

    @Bean
    public ExternalDocumentManager manager() {
        return new ExternalDocumentManager("init_data");
    }
}

在单元测试中,我正在使用springboot TestRestTemplate 测试应用程序,但我不知道如何注入其他 DocumentManager 实现进入我用于这些测试的资源...它仍在注入 ExternalDocumentManager

package com.coriaedu.documentprovider;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import com.coriaedu.documentprovider.interfaces.DocumentManager;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class DocumentProviderApplicationTests {

    @Autowired
    private TestRestTemplate restTemplate;

    @MockBean // this works... but I want to use a test implementation instead of a Mockito implementation.
    DocumentManager manager;

    @Test
    public void simple_test() {
        ResponseEntity<String> entity = this.restTemplate.getForEntity("/documents", String.class);
        assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
    }
}

我尝试使用 DocumentManager TestDocumentManager 实现创建一个单独的 TestConfiguration 类,但是该类仍然无法正常工作

0 个答案:

没有答案