Skip to content

Commit 556dee8

Browse files
amedviedievdavidmorley
authored andcommitted
- Initial commit, added code for article
1 parent 2ee1f4b commit 556dee8

File tree

8 files changed

+156
-0
lines changed

8 files changed

+156
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.class
2+
3+
#folders#
4+
/target
5+
/neoDb*
6+
/data
7+
/src/main/webapp/WEB-INF/classes
8+
*/META-INF/*
9+
10+
# Packaged files #
11+
*.jar
12+
*.war
13+
*.ear

mockito-mocks-spring-beans/pom.xml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>org.baeldung</groupId>
7+
<artifactId>mockito-mocks-spring-beans</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>mocks</name>
12+
<description>Injecting Mockito Mocks into Spring Beans</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.3.1.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter</artifactId>
30+
<version>1.3.1.RELEASE</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-test</artifactId>
35+
<version>1.3.1.RELEASE</version>
36+
<scope>test</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.mockito</groupId>
40+
<artifactId>mockito-all</artifactId>
41+
<version>1.10.19</version>
42+
</dependency>
43+
</dependencies>
44+
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-maven-plugin</artifactId>
50+
</plugin>
51+
</plugins>
52+
</build>
53+
54+
55+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.baeldung;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class MocksApplication {
8+
public static void main(String[] args) {
9+
SpringApplication.run(MocksApplication.class, args);
10+
}
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.baeldung;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
@Service
6+
public class NameService {
7+
public String getUserName(String id) {
8+
return "Baeldung";
9+
}
10+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.baeldung;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Service;
5+
6+
@Service
7+
public class UserService {
8+
9+
private NameService nameService;
10+
11+
@Autowired
12+
public UserService(NameService nameService) {
13+
this.nameService = nameService;
14+
}
15+
16+
public String getUserName(String id) {
17+
return nameService.getUserName(id);
18+
}
19+
}

mockito-mocks-spring-beans/src/main/resources/application.properties

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.baeldung;
2+
3+
import org.mockito.Mockito;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.annotation.Primary;
7+
import org.springframework.context.annotation.Profile;
8+
9+
@Profile("test")
10+
@Configuration
11+
public class NameServiceTestConfiguration {
12+
@Bean
13+
@Primary
14+
public NameService nameService() {
15+
return Mockito.mock(NameService.class);
16+
}
17+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.baeldung;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.mockito.Mockito;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.SpringApplicationConfiguration;
9+
import org.springframework.test.context.ActiveProfiles;
10+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
11+
12+
@ActiveProfiles("test")
13+
@RunWith(SpringJUnit4ClassRunner.class)
14+
@SpringApplicationConfiguration(classes = MocksApplication.class)
15+
public class UserServiceTest {
16+
17+
@Autowired
18+
private UserService userService;
19+
20+
@Autowired
21+
private NameService nameService;
22+
23+
@Test
24+
public void whenUserIdIsProvided_thenRetrievedNameIsCorrect() {
25+
Mockito.when(nameService.getUserName("SomeId")).thenReturn("Baeldung");
26+
27+
String testName = userService.getUserName("SomeId");
28+
29+
Assert.assertEquals("Baeldung", testName);
30+
}
31+
}

0 commit comments

Comments
 (0)