Skip to content

Commit

Permalink
BAEL-7898 - Inject mocks and autowired (eugenp#16514)
Browse files Browse the repository at this point in the history
* BAEL-7646 - converting json node to java collections

* BAEL-7646 - changing test method names

* BAEL-7898 - removing print statements
  • Loading branch information
abh1navv authored May 18, 2024
1 parent b8ff33e commit 0818349
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 0 deletions.
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;
}
}
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);
}

}
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");
}
}
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);
}
}
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"));
}
}
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"));
}

}
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"));
}

}

0 comments on commit 0818349

Please sign in to comment.