forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-7898 - Inject mocks and autowired (eugenp#16514)
* BAEL-7646 - converting json node to java collections * BAEL-7646 - changing test method names * BAEL-7898 - removing print statements
- Loading branch information
Showing
7 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
testing-modules/spring-mockito/src/main/java/com/baeldung/injectmocks/Book.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.baeldung.injectmocks; | ||
|
||
public class Book { | ||
private String id; | ||
private String name; | ||
private String author; | ||
|
||
public Book(String id, String name, String author) { | ||
this.id = id; | ||
this.name = name; | ||
this.author = author; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getAuthor() { | ||
return author; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
testing-modules/spring-mockito/src/main/java/com/baeldung/injectmocks/BookService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.baeldung.injectmocks; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class BookService { | ||
|
||
private DatabaseService databaseService; | ||
private ObjectMapper objectMapper; | ||
|
||
BookService(DatabaseService databaseService, ObjectMapper objectMapper) { | ||
this.databaseService = databaseService; | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
String getBook(String id) throws JsonProcessingException { | ||
Book book = databaseService.findById(id); | ||
return objectMapper.writeValueAsString(book); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
testing-modules/spring-mockito/src/main/java/com/baeldung/injectmocks/DatabaseService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.baeldung.injectmocks; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class DatabaseService { | ||
|
||
public Book findById(String id) { | ||
// querying a Database and getting a book | ||
return new Book("id", "Name", "Author"); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...modules/spring-mockito/src/main/java/com/baeldung/injectmocks/InjectMocksApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.baeldung.injectmocks; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class InjectMocksApplication { | ||
public static void main(String[] args) { | ||
SpringApplication.run(InjectMocksApplication.class, args); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...to/src/test/java/com/baeldung/injectmocks/BookServiceAutowiredAndInjectMocksUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.baeldung.injectmocks; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.when; | ||
|
||
@SpringBootTest | ||
class BookServiceAutowiredAndInjectMocksUnitTest { | ||
|
||
@Mock | ||
private DatabaseService databaseService; | ||
|
||
@Autowired | ||
@InjectMocks | ||
private BookService bookService; | ||
|
||
@Test | ||
void givenBookService_whenGettingBook_thenBookIsCorrect() throws JsonProcessingException { | ||
Book book1 = new Book("1234", "Inferno", "Dan Brown"); | ||
|
||
MockitoAnnotations.openMocks(this); | ||
|
||
when(databaseService.findById(eq("1234"))).thenReturn(book1); | ||
String bookString1 = bookService.getBook("1234"); | ||
Assertions.assertTrue(bookString1.contains("Dan Brown")); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...mockito/src/test/java/com/baeldung/injectmocks/BookServiceMockAndInjectMocksUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.baeldung.injectmocks; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.when; | ||
|
||
@SpringBootTest | ||
class BookServiceMockAndInjectMocksUnitTest { | ||
|
||
@Mock | ||
private DatabaseService databaseService; | ||
|
||
@Mock | ||
private ObjectMapper objectMapper; | ||
|
||
@InjectMocks | ||
private BookService bookService; | ||
|
||
@Test | ||
void givenBookService_whenGettingBook_thenBookIsCorrect() throws JsonProcessingException { | ||
Book book1 = new Book("1234", "Inferno", "Dan Brown"); | ||
when(databaseService.findById(eq("1234"))).thenReturn(book1); | ||
|
||
when(objectMapper.writeValueAsString(any())).thenReturn(new ObjectMapper().writeValueAsString(book1)); | ||
|
||
String bookString1 = bookService.getBook("1234"); | ||
Assertions.assertTrue(bookString1.contains("Dan Brown")); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...ckito/src/test/java/com/baeldung/injectmocks/BookServiceMockBeanAndAutowiredUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.baeldung.injectmocks; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
|
||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.when; | ||
|
||
@SpringBootTest | ||
class BookServiceMockBeanAndAutowiredUnitTest { | ||
|
||
@MockBean | ||
private DatabaseService databaseService; | ||
@Autowired | ||
private BookService bookService; | ||
|
||
@Test | ||
void givenBookService_whenGettingBook_thenBookIsCorrect() throws JsonProcessingException { | ||
Book book1 = new Book("1234", "Inferno", "Dan Brown"); | ||
when(databaseService.findById(eq("1234"))).thenReturn(book1); | ||
|
||
String bookString1 = bookService.getBook("1234"); | ||
Assertions.assertTrue(bookString1.contains("Dan Brown")); | ||
} | ||
|
||
} |