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

Refactor #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "interactive"
}
52 changes: 52 additions & 0 deletions API-Spring-boot-mockito-[Mongodb].postman_collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"info": {
"_postman_id": "e480e0ca-b488-4000-ae6c-c4f94410991b",
"name": "API-Spring-boot-mockito-[Mongodb]",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Users - save",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"name\":\"Fabio Junior\",\r\n \"age\": 31,\r\n \"address\":\"Rua Alvarez\"\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "localhost:8080/save",
"host": [
"localhost"
],
"port": "8080",
"path": [
"save"
]
}
},
"response": []
},
{
"name": "Users - getUsers",
"request": {
"method": "GET",
"header": []
},
"response": []
},
{
"name": "Users - getUserByAddress",
"request": {
"method": "GET",
"header": []
},
"response": []
}
]
}
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
# spring-boot-mockito
How to write Unit test case using mockito framework

#Atualização para evitar erro de duplicação de chave do mongodb#
- mudamos o tipo do id do usuário para String para evitar duplicação de chave
- mudamos o teste do controler do usuário para a pasta específica, desacoplando da pasta da aplicação
- testamos a api no Postaman com sucesso

#Geração de endpoint#
- foi gerado o endpoint caso necessite testar
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/javatechie/spring/mockito/api/model/User.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
package com.javatechie.spring.mockito.api.model;

import org.bson.types.ObjectId;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Document(collection = "users")
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Getter
@Setter
public class User {
@Id
private int id;
private String id;
private String name;
private int age;
private String address;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,61 +1,15 @@
package com.javatechie.spring.mockito.api;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;

import com.javatechie.spring.mockito.api.dao.UserRepository;
import com.javatechie.spring.mockito.api.model.User;
import com.javatechie.spring.mockito.api.service.UserService;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootMockitoApplicationTests {

@Autowired
private UserService service;

@MockBean
private UserRepository repository;

@Test
public void getUsersTest() {
when(repository.findAll()).thenReturn(Stream
.of(new User(376, "Danile", 31, "USA"), new User(958, "Huy", 35, "UK")).collect(Collectors.toList()));
assertEquals(2, service.getUsers().size());
@Test
public void contextLoads() {
}

@Test
public void getUserbyAddressTest() {
String address = "Bangalore";
when(repository.findByAddress(address))
.thenReturn(Stream.of(new User(376, "Danile", 31, "USA")).collect(Collectors.toList()));
assertEquals(1, service.getUserbyAddress(address).size());
}

@Test
public void saveUserTest() {
User user = new User(999, "Pranya", 33, "Pune");
when(repository.save(user)).thenReturn(user);
assertEquals(user, service.addUser(user));
}

@Test
public void deleteUserTest() {
User user = new User(999, "Pranya", 33, "Pune");
service.deleteUser(user);
verify(repository, times(1)).delete(user);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.javatechie.spring.mockito.api.controller;

import static org.junit.Assert.*;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;

import com.javatechie.spring.mockito.api.dao.UserRepository;
import com.javatechie.spring.mockito.api.model.User;
import com.javatechie.spring.mockito.api.service.UserService;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserControllerTest {
@Autowired
private UserService service;

@MockBean
private UserRepository repository;

@Test
public void getUsersTest() {
when(repository.findAll())
.thenReturn(Stream.of(new User("376", "Danile", 31, "USA"), new User("958", "Huy", 35, "UK"))
.collect(Collectors.toList()));
assertEquals(2, service.getUsers().size());
}

@Test
public void getUserbyAddressTest() {
String address = "Bangalore";
when(repository.findByAddress(address))
.thenReturn(Stream.of(new User("376", "Danile", 31, "USA")).collect(Collectors.toList()));
assertEquals(1, service.getUserbyAddress(address).size());
}

@Test
public void saveUserTest() {
User user = new User("999", "Pranya", 33, "Pune");
when(repository.save(user)).thenReturn(user);
assertEquals(user, service.addUser(user));
}

@Test
public void deleteUserTest() {
User user = new User("999", "Pranya", 33, "Pune");
service.deleteUser(user);
verify(repository, times(1)).delete(user);
}

}
3 changes: 3 additions & 0 deletions target/classes/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=USERDB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 4 additions & 0 deletions target/maven-archiver/pom.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#Created by Apache Maven 3.8.5
version=0.0.1-SNAPSHOT
groupId=com.example
artifactId=spring-boot-mockito
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
com\javatechie\spring\mockito\api\SpringBootMockitoApplication.class
com\javatechie\spring\mockito\api\controller\UserController.class
com\javatechie\spring\mockito\api\dao\UserRepository.class
com\javatechie\spring\mockito\api\model\User.class
com\javatechie\spring\mockito\api\service\UserService.class
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
C:\Users\fabio.batista\Desktop\API-spring-mongo-test\branch-fabiojcbatista\spring-boot-mockito\src\main\java\com\javatechie\spring\mockito\api\controller\UserController.java
C:\Users\fabio.batista\Desktop\API-spring-mongo-test\branch-fabiojcbatista\spring-boot-mockito\src\main\java\com\javatechie\spring\mockito\api\dao\UserRepository.java
C:\Users\fabio.batista\Desktop\API-spring-mongo-test\branch-fabiojcbatista\spring-boot-mockito\src\main\java\com\javatechie\spring\mockito\api\service\UserService.java
C:\Users\fabio.batista\Desktop\API-spring-mongo-test\branch-fabiojcbatista\spring-boot-mockito\src\main\java\com\javatechie\spring\mockito\api\model\User.java
C:\Users\fabio.batista\Desktop\API-spring-mongo-test\branch-fabiojcbatista\spring-boot-mockito\src\main\java\com\javatechie\spring\mockito\api\SpringBootMockitoApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
com\javatechie\spring\mockito\api\controller\UserControllerTest.class
com\javatechie\spring\mockito\api\SpringBootMockitoApplicationTests.class
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
C:\Users\fabio.batista\Desktop\API-spring-mongo-test\branch-fabiojcbatista\spring-boot-mockito\src\test\java\com\javatechie\spring\mockito\api\SpringBootMockitoApplicationTests.java
C:\Users\fabio.batista\Desktop\API-spring-mongo-test\branch-fabiojcbatista\spring-boot-mockito\src\test\java\com\javatechie\spring\mockito\api\controller\UserControllerTest.java
Binary file added target/spring-boot-mockito-0.0.1-SNAPSHOT.jar
Binary file not shown.
Binary file not shown.
15 changes: 15 additions & 0 deletions target/surefire-reports/2022-03-13T16-47-21_366-jvmRun1.dump
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Created at 2022-03-13T16:47:31.285
System.exit() or native command error interrupted process checker.
java.lang.IllegalStateException: error [STOPPED] to read process 16148
at org.apache.maven.surefire.booter.PpidChecker.checkProcessInfo(PpidChecker.java:145)
at org.apache.maven.surefire.booter.PpidChecker.isProcessAlive(PpidChecker.java:116)
at org.apache.maven.surefire.booter.ForkedBooter$2.run(ForkedBooter.java:214)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)


Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-------------------------------------------------------------------------------
Test set: com.javatechie.spring.mockito.api.SpringBootMockitoApplicationTests
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.162 s - in com.javatechie.spring.mockito.api.SpringBootMockitoApplicationTests
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-------------------------------------------------------------------------------
Test set: com.javatechie.spring.mockito.api.controller.UserControllerTest
-------------------------------------------------------------------------------
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 4.949 s - in com.javatechie.spring.mockito.api.controller.UserControllerTest
Binary file not shown.
Binary file not shown.