Skip to content

Commit

Permalink
Added unit tests and integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sainik73 committed Apr 5, 2021
1 parent f1fd4f7 commit e440984
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 23 deletions.
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,21 @@

<properties>
<java.version>1.8</java.version>
<testcontainers.version>1.15.0</testcontainers.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>${testcontainers.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -52,6 +65,17 @@
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
<!-- Test dependency for Mongo -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mongodb</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/io.swagger.core.v3/swagger-annotations -->
<dependency>
Expand Down
3 changes: 0 additions & 3 deletions src/main/resources/application.properties

This file was deleted.

31 changes: 15 additions & 16 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ spring:
name: students-api
data:
mongodb:
#uri: mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOST}/${MONGO_DB}
database: '${MONGO_DB}'
host: '${MONGO_HOST}'
port: 27017
username: '${MONGO_USERNAME}'
password: '${MONGO_PASSWORD}'
#authentication-database: admin
management:
endpoint:
metrics:
enabled: true
loggers:
enabled: true
health:
enabled: true
info:
enabled: true
host: '${MONGO_HOST}'
port: 27017
username: '${MONGO_USERNAME}'
password: '${MONGO_PASSWORD}'
#authentication-database: admin
management:
endpoint:
metrics:
enabled: true
loggers:
enabled: true
health:
enabled: true
info:
enabled: true
Original file line number Diff line number Diff line change
@@ -1,16 +1,108 @@
package com.example.openapi.students;

import com.example.openapi.model.Student;
import com.example.openapi.students.controller.StudentsApiController;
import com.example.openapi.students.repository.StudentsRepository;
import com.example.openapi.students.service.StudentsService;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.micrometer.core.instrument.util.JsonUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.*;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;

@Disabled
@SpringBootTest
import static org.mockito.ArgumentMatchers.any;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
import static org.springframework.http.RequestEntity.post;

@WebMvcTest
@AutoConfigureMockMvc
class StudentsApplicationTests {

@Disabled
@Test()
@MockBean
private StudentsApiController studentsApiController;

@MockBean
StudentsService studentsService;
@Autowired
private MockMvc mockMvc;

/* Test that context is loaded */
@Test
void contextLoads() {
assertThat(studentsApiController).isNotNull();
}

@Test
void testGetStudent() throws Exception {
Student student = new Student();
student.setStudentid(1);
student.setName("Test");
student.setAge(22);
when(studentsApiController.findStudentById(1))
.thenReturn(new ResponseEntity<>(student, HttpStatus.OK));
this.mockMvc.perform(get("/students/1")).
andDo(print()).
andExpect(status().isOk()).
andExpect(MockMvcResultMatchers.jsonPath("$.size()").value(3)).
andExpect(MockMvcResultMatchers.jsonPath("$.studentid").value(1));
}

@Test
void testCreateStudent() throws Exception {
Student student = new Student();
student.setStudentid(1);
student.setName("Test");
student.setAge(22);
when(studentsApiController.createStudent(any(Student.class))).thenReturn(new ResponseEntity<>(HttpStatus.OK));

ResultActions resultActions =
mockMvc.perform(MockMvcRequestBuilders.post("/students").
content(writeAsJsonString(student)).
contentType(MediaType.APPLICATION_JSON)
);
resultActions.
andExpect(MockMvcResultMatchers.status().isOk()).
andDo(MockMvcResultHandlers.print());
}

/**
* Utility method to create JSON string
* @param obj Object to convert to json
* @return String
*/
static String writeAsJsonString(final Object obj) {
try {
final ObjectMapper mapper = new ObjectMapper();
final String jsonContent = mapper.writeValueAsString(obj);
System.out.println(jsonContent);
return jsonContent;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit e440984

Please sign in to comment.