Skip to content

Commit

Permalink
#1 add initial mockito testing
Browse files Browse the repository at this point in the history
  • Loading branch information
obriensystems committed Sep 8, 2020
1 parent 60fbadb commit 1e54ae6
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 10 deletions.
11 changes: 9 additions & 2 deletions magellan-nbi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@
<java.version>11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<jackson-2-version>2.9.8</jackson-2-version>
<jackson-2-version>2.10.1</jackson-2-version>
<aws-secrets-manager-version>1.11.339</aws-secrets-manager-version>
<jersey.version>2.27</jersey.version>
</properties>

<dependencies>
<!-- AWS -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-secretsmanager</artifactId>
<version>${aws-secrets-manager-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<!--artifactId>spring-boot-starter-data-jpa</artifactId-->
Expand All @@ -46,7 +53,7 @@
<scope>test</scope>
</dependency>


<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package global.packet.magellan;
package global.packet.magellan.controller;


public class Api {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package global.packet.magellan;
package global.packet.magellan.controller;

import java.util.concurrent.atomic.AtomicLong;

Expand All @@ -12,6 +12,8 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import global.packet.magellan.service.ApplicationServiceLocal;

@Controller
@RequestMapping("/api")
// http://localhost:8080/nbi/api
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package global.packet.magellan;
package global.packet.magellan.controller;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
Expand All @@ -12,6 +12,9 @@
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import global.packet.magellan.service.ApplicationService;
import global.packet.magellan.service.ApplicationServiceLocal;

//import com.wordnik.swagger.annotations.ApiOperation;
//import com.wordnik.swagger.annotations.ApiResponse;
//import com.wordnik.swagger.annotations.ApiResponses;
Expand All @@ -21,7 +24,7 @@
public class ForwardingController {

@Autowired
ApplicationServiceLocal applicationService;
ApplicationService applicationService;

//@GET
//@Path("/health")
Expand All @@ -37,7 +40,7 @@ public class ForwardingController {
@ApiResponse(code=409, message="Conflict"),
@ApiResponse(code=500, message="Internal Server Error")
})*/
@GetMapping("/forward")
@GetMapping("/health")
public String getHealth() {
return applicationService.health().toString();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package global.packet.magellan;
package global.packet.magellan.service;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Component
@Service
public class ApplicationService implements ApplicationServiceLocal {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package global.packet.magellan;
package global.packet.magellan.service;

public interface ApplicationServiceLocal {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package global.packet.magellan;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import global.packet.magellan.controller.ForwardingController;

public class ForwardingControllerTest {
// controller is unmocked, but service bean inside is mocked
private @InjectMocks ForwardingController controller = Mockito.mock(ForwardingController.class);
// if controller is InjectMocked then Mock service bean will be injected
//@Mock ForwardingController controller;

@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}

@Test
public void testForwardRequest() {
String expected = "ok";
Mockito.when(controller.getHealth())
.thenReturn(expected);
String response = controller.getHealth();
Assertions.assertNotNull(response);
Assert.assertEquals(expected, response);
}

}

0 comments on commit 1e54ae6

Please sign in to comment.