Tomcat Mock Server

Helper code to spin up a tomcat app during tests and mocking downstream http calls

Лицензия

Лицензия

Категории

Категории

Tomcat Контейнер Application Servers
Группа

Группа

com.dthoffman.tomcatmock
Идентификатор

Идентификатор

tomcat-test-app-server
Последняя версия

Последняя версия

0.0.1
Дата

Дата

Тип

Тип

jar
Описание

Описание

Tomcat Mock Server
Helper code to spin up a tomcat app during tests and mocking downstream http calls
Ссылка на сайт

Ссылка на сайт

https://github.com/dhoff1985/tomcat-mock-server
Система контроля версий

Система контроля версий

https://github.com/dhoff1985/tomcat-mock-server

Скачать tomcat-test-app-server

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/com.dthoffman.tomcatmock/tomcat-test-app-server/ -->
<dependency>
    <groupId>com.dthoffman.tomcatmock</groupId>
    <artifactId>tomcat-test-app-server</artifactId>
    <version>0.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.dthoffman.tomcatmock/tomcat-test-app-server/
implementation 'com.dthoffman.tomcatmock:tomcat-test-app-server:0.0.1'
// https://jarcasting.com/artifacts/com.dthoffman.tomcatmock/tomcat-test-app-server/
implementation ("com.dthoffman.tomcatmock:tomcat-test-app-server:0.0.1")
'com.dthoffman.tomcatmock:tomcat-test-app-server:jar:0.0.1'
<dependency org="com.dthoffman.tomcatmock" name="tomcat-test-app-server" rev="0.0.1">
  <artifact name="tomcat-test-app-server" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.dthoffman.tomcatmock', module='tomcat-test-app-server', version='0.0.1')
)
libraryDependencies += "com.dthoffman.tomcatmock" % "tomcat-test-app-server" % "0.0.1"
[com.dthoffman.tomcatmock/tomcat-test-app-server "0.0.1"]

Зависимости

compile (7)

Идентификатор библиотеки Тип Версия
org.apache.tomcat.embed : tomcat-embed-core jar 8.0.28
org.reflections : reflections jar 0.9.10
org.apache.tomcat : tomcat-jsp-api jar 8.0.28
org.apache.tomcat.embed : tomcat-embed-logging-juli jar 8.0.28
org.apache.tomcat : tomcat-jasper-el jar 8.0.28
org.apache.tomcat.embed : tomcat-embed-jasper jar 8.0.28
org.apache.tomcat : tomcat-jasper jar 8.0.28

Модули Проекта

Данный проект не имеет модулей.

Tomcat Mock Server

This project aims to help you write integration tests for a web application that run as a part of your build. It includes code to help start an embedded tomcat instance with the application under test deployed and also an additional embedded tomcat that allows you to mock out any http dependencies.

Example:

You have a rest application that deploys to tomcat as a war. This rest application has a service "/joke" which uses the internet chuck norris database (icndb) to retrieve a joke. You can integration test this service using a regular unit test (see junit-example for more complete picture):

// service code:
@RestController
public class JokeAppController {

    @Autowired
    HttpClient httpClient;

    @Value("${joke.baseUri}")
    String jokeBaseUri;

    @Autowired
    ObjectMapper objectMapper;

    @RequestMapping(path = "/joke", method = RequestMethod.GET)
    ResponseEntity<String> getMeAJoke() throws URISyntaxException, IOException {
        HttpGet get = new HttpGet(new URIBuilder(jokeBaseUri).setPath("/jokes/random/").build());
        HttpResponse response = httpClient.execute(get);
        JokeResponse jokeResponse = objectMapper.readValue(response.getEntity().getContent(), JokeResponse.class);
        return ResponseEntity.status(200).body(jokeResponse.getValue().getJoke());
    }
}



// Test code:
    private static final String JOKE_TEXT = "4 out of 5 doctors fail to recommend Chuck Norris as a solution to most problems. Also, 80% of doctors die unexplained, needlessly brutal deaths.";

    @Test
    public void testJokeRestService() throws IOException {
        TomcatAppServer tomcatAppServer;
        tomcatAppServer = new TomcatAppServer();
        tomcatAppServer.setPort(4831);
        tomcatAppServer.setServletInitializerClass(SpringServletContainerInitializer.class);
        tomcatAppServer.start();

        TomcatMockServer tomcatMockServer;
        tomcatMockServer = new TomcatMockServer();
        tomcatMockServer.setPort(5831);
        tomcatMockServer.start();

        TomcatMock tomcatMock = mock(TomcatMock.class);
        tomcatMockServer.setTomcatMock(tomcatMock);

        JokeResponse jokeResponse = new JokeResponse("success", 1, JOKE_TEXT, new ArrayList<String>());

        TomcatResponse icndbResponse = TomcatResponse.status(200).body(objectMapper.writeValueAsBytes(jokeResponse)).build();
        when(tomcatMock.method(eq(HttpMethod.GET), eq("/jokes/random/"), any(HttpServletRequest.class))).thenReturn(icndbResponse);

        HttpResponse response = HttpClientBuilder.create().build().execute(new HttpGet("http://localhost:4831/joke"));
        String jokeText = IOUtils.toString(response.getEntity().getContent());

        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals(JOKE_TEXT, jokeText);

        tomcatMockServer.stop();
        tomcatAppServer.stop();
    }
  1. First you start up an embedded tomcat. This one uses SpringServletContainerInitializer which will pick up any WebApplicationInitializer classes on the classpath.
  2. Next you can start up an embedded tomcat to act as the downstream service (TomcatMockServer),
  3. You can put a mock of TomcatMock into the mock server and use a mocking library like mockito to mock interctions.
  4. This mock returns a TomcatResponse instance with a status of 200 and a json body with a joke inside.
  5. Be sure to stop your servers when finished.

It may also be optimal to start these servers at the beginning of a suite and stop them at the end, just make sure you don't dirty any context between tests.

Версии библиотеки

Версия
0.0.1