Skip to content

Commit f2e8f94

Browse files
committed
Junit5
1 parent 8c7e2f1 commit f2e8f94

8 files changed

Lines changed: 254 additions & 110 deletions

File tree

pom.xml

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
<properties>
1818
<java.version>1.8</java.version>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1920
</properties>
2021

2122
<dependencies>
@@ -43,31 +44,43 @@
4344
<scope>test</scope>
4445
<exclusions>
4546
<exclusion>
46-
<groupId>org.junit.vintage</groupId>
47-
<artifactId>junit-vintage-engine</artifactId>
48-
</exclusion>
47+
<groupId>junit</groupId>
48+
<artifactId>junit</artifactId>
49+
</exclusion>
50+
51+
<exclusion>
52+
<groupId>org.junit.vintage</groupId>
53+
<artifactId>junit-vintage-engine</artifactId>
54+
</exclusion>
55+
4956
</exclusions>
5057
</dependency>
5158
<dependency>
5259
<groupId>org.projectlombok</groupId>
5360
<artifactId>lombok</artifactId>
5461
</dependency>
55-
<!-- https://mvnrepository.com/artifact/junit/junit -->
5662
<dependency>
57-
<groupId>junit</groupId>
58-
<artifactId>junit</artifactId>
59-
<version>4.13</version>
63+
<groupId>org.mockito</groupId>
64+
<artifactId>mockito-all</artifactId>
65+
<version>1.9.5</version>
6066
<scope>test</scope>
6167
</dependency>
62-
63-
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
64-
<dependency>
65-
<groupId>org.mockito</groupId>
66-
<artifactId>mockito-all</artifactId>
67-
<version>1.9.5</version>
68-
<scope>test</scope>
69-
</dependency>
70-
68+
<dependency>
69+
<groupId>org.junit.platform</groupId>
70+
<artifactId>junit-platform-launcher</artifactId>
71+
<version>1.5.1</version>
72+
<scope>test</scope>
73+
</dependency>
74+
<dependency>
75+
<groupId>org.junit.jupiter</groupId>
76+
<artifactId>junit-jupiter-api</artifactId>
77+
<scope>test</scope>
78+
</dependency>
79+
<dependency>
80+
<groupId>org.junit.jupiter</groupId>
81+
<artifactId>junit-jupiter-engine</artifactId>
82+
<scope>test</scope>
83+
</dependency>
7184

7285
</dependencies>
7386

@@ -79,40 +92,26 @@
7992
</plugin>
8093

8194
<plugin>
82-
<groupId>org.jacoco</groupId>
83-
<artifactId>jacoco-maven-plugin</artifactId>
84-
<version>0.8.2</version>
85-
<executions>
86-
<!-- Prepares the property pointing to the JaCoCo runtime agent which is passed as VM argument when Maven the Surefire plugin is executed. -->
87-
<execution>
88-
<id>pre-unit-test</id>
89-
<goals>
90-
<goal>prepare-agent</goal>
91-
</goals>
92-
<configuration>
93-
<!-- Sets the path to the file which contains the execution data. -->
94-
<destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
95-
<!-- Sets the name of the property containing the settings for JaCoCo runtime agent. -->
96-
<propertyName>surefireArgLine</propertyName>
97-
</configuration>
98-
</execution>
99-
<!-- Ensures that the code coverage report for unit tests is created after unit tests have been run. -->
100-
<execution>
101-
<id>post-unit-test</id>
102-
<phase>test</phase>
103-
<goals>
104-
<goal>report</goal>
105-
</goals>
106-
<configuration>
107-
<!-- Sets the path to the file which contains the execution data. -->
108-
<dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
109-
<!-- Sets the output directory for the code coverage report. -->
110-
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
111-
</configuration>
112-
</execution>
113-
</executions>
114-
</plugin>
115-
95+
<groupId>org.jacoco</groupId>
96+
<artifactId>jacoco-maven-plugin</artifactId>
97+
<version>0.8.5</version>
98+
<executions>
99+
<execution>
100+
<goals>
101+
<goal>prepare-agent</goal>
102+
</goals>
103+
</execution>
104+
105+
<execution>
106+
<id>report</id>
107+
<phase>test</phase>
108+
<goals>
109+
<goal>report</goal>
110+
</goals>
111+
</execution>
112+
</executions>
113+
</plugin>
114+
116115
</plugins>
117116
</build>
118117

src/main/java/com/example/demo/controller/LocationController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
@RequestMapping({"/location"})
2121
@CrossOrigin(origins = "http://localhost:4200",maxAge = 3600)
2222
public class LocationController {
23+
2324
@Autowired
2425
LocationServiceImp service;
2526

src/test/java/com/example/demoLocationDB/LocationTest.java renamed to src/test/java/com/example/demo/LocationDB/LocationTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1-
package com.example.demoLocationDB;
2-
3-
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertNotNull;
5-
import org.junit.jupiter.api.Test;
6-
7-
8-
import org.junit.Before;
9-
10-
import com.example.demo.LocationDB.Location;
1+
package com.example.demo.LocationDB;
112

3+
import static org.junit.jupiter.api.Assertions.*;
124

5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
import org.springframework.beans.factory.annotation.Autowired;
138

14-
public class LocationTest {
9+
class LocationTest {
10+
11+
@Autowired
12+
Location location;
13+
1514
private int id=1;
1615
private String name="Test";
1716
private int area_m2=100;
18-
private Location location;
1917

20-
@Before
18+
@BeforeEach
2119
public void setUpLocation(){
20+
location = new Location(id, name, area_m2);
2221
location.setId(id);
2322
location.setName(name);
2423
location.setArea_m2(area_m2);
2524
System.out.println("Entro Before");
26-
}
27-
25+
}
2826

2927
@Test
3028
public void with_parameterized_constructor() throws Exception{
31-
System.out.println("Entro Test 2");
3229
location = new Location(id, name, area_m2);
3330
assertNotNull(location);
3431
assertEquals(id, location.getId());
3532
assertEquals(name, location.getName());
3633
assertEquals(area_m2, location.getArea_m2());
3734
}
35+
36+
37+
3838
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.example.demo;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
class Test1ApplicationTest {
6+
7+
@Test
8+
void contextLoads() {
9+
}
10+
}

src/test/java/com/example/demo/Test1ApplicationTests.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.example.demo.controller;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
import org.mockito.InjectMocks;
11+
import org.mockito.Mock;
12+
import org.mockito.Mockito;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
15+
import org.springframework.boot.test.mock.mockito.MockBean;
16+
import org.springframework.test.web.servlet.MockMvc;
17+
18+
import com.example.demo.LocationDB.Location;
19+
import com.example.demo.service.ILocationRepo;
20+
import com.example.demo.service.LocationServiceImp;
21+
import com.fasterxml.jackson.databind.ObjectMapper;
22+
23+
@WebMvcTest
24+
class LocationControllerTest {
25+
26+
@Autowired
27+
private MockMvc mocMvc;
28+
@Autowired
29+
private ObjectMapper objectMapper;
30+
31+
@Mock
32+
private LocationServiceImp service;
33+
34+
@MockBean
35+
private ILocationRepo repositorio;
36+
37+
@InjectMocks
38+
private LocationController locationController;
39+
40+
Location location;
41+
Location location2;
42+
private int id=1;
43+
private String name="Test";
44+
private int area_m2=100;
45+
private int id2=1;
46+
private String name2="Test";
47+
private int area_m2_2=100;
48+
49+
@BeforeEach
50+
public void setUpLocation(){
51+
location = new Location(id, name, area_m2);
52+
location.setId(id);
53+
location.setName(name);
54+
location.setArea_m2(area_m2);
55+
56+
location2 = new Location(id, name, area_m2);
57+
location2.setId(id2);
58+
location2.setName(name2);
59+
location2.setArea_m2(area_m2_2);
60+
}
61+
62+
63+
@Test
64+
void listarTest() throws Exception{
65+
List<Location> listLocation= new ArrayList<>();
66+
listLocation.add(location);
67+
listLocation.add(location2);
68+
Mockito.when(service.listar()).thenReturn(listLocation);
69+
assertNotNull(listLocation);
70+
assertEquals(id, listLocation.get(id).getId());
71+
assertEquals(name, listLocation.get(id).getName());
72+
assertEquals(area_m2, listLocation.get(id).getArea_m2());
73+
assertEquals(id2, listLocation.get(id2).getId());
74+
assertEquals(name2, listLocation.get(id2).getName());
75+
assertEquals(area_m2_2, listLocation.get(id2).getArea_m2());
76+
}
77+
@Test
78+
void ListarIDtest() throws Exception{
79+
Mockito.when(service.listarID(id)).thenReturn(location);
80+
assertNotNull(location);
81+
assertEquals(id, location.getId());
82+
assertEquals(name, location.getName());
83+
assertEquals(area_m2, location.getArea_m2());
84+
}
85+
@Test
86+
void agregarTest() throws Exception{
87+
Mockito.when(service.add(location)).thenReturn(null);
88+
}
89+
@Test
90+
void eliminarTest() throws Exception{
91+
Mockito.when(service.delete(id)).thenReturn(null);
92+
}
93+
94+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.example.demo.service;
2+
3+
import java.util.ArrayList;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertNotNull;
7+
import static org.mockito.ArgumentMatchers.any;
8+
import java.util.List;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
11+
import org.mockito.InjectMocks;
12+
import org.mockito.Mockito;
13+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
14+
import org.springframework.boot.test.mock.mockito.MockBean;
15+
16+
import com.example.demo.LocationDB.Location;
17+
import com.example.demo.controller.LocationController;
18+
19+
@WebMvcTest
20+
class LocationServiceImpTest {
21+
22+
@MockBean
23+
private ILocationRepo repositorio;
24+
25+
@InjectMocks
26+
private LocationServiceImp service;
27+
28+
Location location;
29+
Location location2;
30+
private int id=1;
31+
private String name="Test";
32+
private int area_m2=100;
33+
private int id2=1;
34+
private String name2="Test";
35+
private int area_m2_2=100;
36+
37+
@BeforeEach
38+
public void setUpLocation(){
39+
location = new Location(id, name, area_m2);
40+
location.setId(id);
41+
location.setName(name);
42+
location.setArea_m2(area_m2);
43+
44+
location2 = new Location(id, name, area_m2);
45+
location2.setId(id2);
46+
location2.setName(name2);
47+
location2.setArea_m2(area_m2_2);
48+
49+
}
50+
51+
52+
@Test
53+
void ListarIDtest() throws Exception{
54+
Mockito.when(repositorio.findById(id)).thenReturn(location);
55+
assertNotNull(location);
56+
assertEquals(id, location.getId());
57+
assertEquals(name, location.getName());
58+
assertEquals(area_m2, location.getArea_m2());
59+
}
60+
61+
62+
@Test
63+
void addtest() throws Exception{
64+
Mockito.when(repositorio.save(location)).thenReturn(null);
65+
}
66+
67+
68+
@Test
69+
void ListAlltest() throws Exception{
70+
List<Location> listLocation= new ArrayList<>();
71+
listLocation.add(location);
72+
listLocation.add(location2);
73+
Mockito.when(repositorio.findAll()).thenReturn(listLocation);
74+
assertNotNull(listLocation);
75+
assertEquals(id, listLocation.get(id).getId());
76+
assertEquals(name, listLocation.get(id).getName());
77+
assertEquals(area_m2, listLocation.get(id).getArea_m2());
78+
assertEquals(id2, listLocation.get(id2).getId());
79+
assertEquals(name2, listLocation.get(id2).getName());
80+
assertEquals(area_m2_2, listLocation.get(id2).getArea_m2());
81+
}
82+
83+
84+
}

0 commit comments

Comments
 (0)