Skip to content

Commit

Permalink
Controller unit tests (#32)
Browse files Browse the repository at this point in the history
* Adding unit tests for controllers

* Update pom.xml

version bump

* Update README.md

* Adding screenshots

* Update README.md
  • Loading branch information
conorheffron authored Jul 17, 2024
1 parent 34a17fd commit 9d76a73
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 38 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,23 @@ Build / Run App:
mvn clean package spring-boot:run
```

Build / Run Docker container:
![image](screen-grabs/IDEA-Intellj-run.png)


Build / Run (spin-up) Docker container:
```
docker image build -t ironoc .
docker compose up -d
```
![image](screen-grabs/cli-docker.png)

Spin-up Container:
```
docker-compose up -d
```

Tear-down:
```
docker-compose down
```

# Screenshot
![Home](https://github.com/conorheffron/ironoc/assets/8218626/69cdb496-9773-4193-b465-92bc05a30800)
![Home](screen-grabs/home-page.png)


2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.ironoc.portfolio</groupId>
<artifactId>ironoc-personal-portfolio</artifactId>
<version>3.1.0</version>
<version>3.1.1</version>
<packaging>war</packaging>

<distributionManagement>
Expand Down
Binary file added screen-grabs/IDEA-Intellj-run.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screen-grabs/cli-docker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screen-grabs/home-page.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ public String index(HttpServletRequest httpServletRequest) {
LOGGER.info(httpServletRequest.getRequestURI() + " page request");
return "index";
}

}
30 changes: 0 additions & 30 deletions src/test/java/com/ironoc/portfolio/TestConfiguration.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.ironoc.portfolio.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.web.servlet.view.RedirectView;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.class)
public class CustomErrorControllerTest {

@InjectMocks
private CustomErrorController customErrorController;

@Mock
private HttpServletRequest httpServletRequestMock;

@Mock
private HttpServletResponse httpServletResponseMock;

@Test
public void test_error_view_success() {
// when
RedirectView result = customErrorController.error(httpServletRequestMock, httpServletResponseMock);

// then
assertThat(result, is(notNullValue()));
assertThat(result, isA(RedirectView.class));
}

@Test
public void test_getErrorPath_success() {
// when
String result = customErrorController.getErrorPath();

// then
assertThat(result, is("/error"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.ironoc.portfolio.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class HomeControllerTest {

@InjectMocks
private HomeController homeController;

@Mock
private HttpServletRequest httpServletRequestMock;

@Test
public void test_index_success() {
// when
String result = homeController.index(httpServletRequestMock);

// then
verify(httpServletRequestMock).getRequestURI();

assertThat(result, is("index"));
}
}

0 comments on commit 9d76a73

Please sign in to comment.