Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write unit tests #22

Merged
merged 3 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,41 @@
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.testing.aem-mock.junit5</artifactId>
<version>5.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>4.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.wttech.stubway.response;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.nio.charset.Charset;

import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.Test;

import io.wttech.stubway.stub.Stub;

public class StubResponseTest {

@Test
public void emptyShouldReturnStubResponseWithEmptyParams() throws IOException {
StubResponse empty = StubResponse.empty();
Assertions.assertEquals(404, empty.getStatusCode());
String expected = "{\n"
+ " \"message\": \"Stub not found\",\n"
+ " \"statusCode\": 404\n"
+ "}";
Assertions.assertEquals(expected, IOUtils.toString(empty.getInputStream(), Charset.defaultCharset()));
}

@Test
public void internalErrorShouldReturnStubResponseWithStatusCode500() {
StubResponse internalError = StubResponse.internalError(new Exception());
Assertions.assertEquals(500, internalError.getStatusCode());
}

@Test
public void errorShouldReturnStubResponseWithStatusCode400() {
StubResponse error = StubResponse.error(new Exception());
Assertions.assertEquals(400, error.getStatusCode());
}

@Test
public void foundStubShouldReturnStubResponseWithSetValues() throws IOException {
Stub stub = mock(Stub.class);
when(stub.getStatusCode()).thenReturn(200);
when(stub.getInputStream()).thenReturn(IOUtils.toInputStream("fake input stream message", Charset.defaultCharset()));
StubResponse foundStub = StubResponse.foundStub(stub);
renata-zuranska-wttech marked this conversation as resolved.
Show resolved Hide resolved
Assertions.assertEquals(200, foundStub.getStatusCode());
Assertions.assertEquals("fake input stream message",
IOUtils.toString(foundStub.getInputStream(), Charset.defaultCharset()));
}
}