@Autowired
above the field.@Autowired
.
@Service
public class BookService{
@Autowired
private BookDao bookDao;
@Autowired
private HogeDao hogeDao;
/**
*Returns a Book that matches the argument bookId
*/
public Book selectById(int bookId){
Optional<Book> wrappedBook = bookDao.findById(bookId);
return wrappedBook.orElseThrow(() -> NotFoundException("No"));
}
.
.
.
}
@Service
public class BookService{
private final BookDao bookDao;
private final HogeDao hogeDao;
@Autowired // → Spring4.This annotation can be omitted if it is 3 or more
public BookService(BookDao bookDao, HogeDao hogeDao){
this.bookDao = bookdao;
this.hogeDao = hogedao;
}
/**
*Returns a Book that matches the argument bookId
*/
public Book selectById(int bookId){
Optional<Book> wrappedBook = bookDao.findById(bookId);
return wrappedBook.orElseThrow(() -> NotFoundException("No"));
}
.
.
.
}
@ Autowired
can be mocked by using @ MockBean
provided by Spring Boot.@RunWith (SpringRunner.class)
and @SpringBootTest
are required.@RunWith(SpringRunner.class)
@SpringBootTest
public class BookServiceTests{
@Autowired
private BookService bookService;
@MockBean
private BookDao bookDao;
@MockBean
private HogeDao hogeDao;
@Test
public void Being able to get books with the specified bookId(){
// setUp
var expected = new Book(1, "Book name");
when(bookDao.findById(1)).thenReturn(expected);
var bookService = new BookService();
// execute
var actual = bookService.selectById(1);
// verify
assertThat(...Omitted below
}
}
@MockBean
, but Spring Boot Test takes a lot of time for unit testing ...Failed to create application context
. (Experienced)@ Table
, it is automatically generated ...?@Before
method.@RunWith (SpringRunner.class)
and @SpringBootTest
were required for the test implemented by field injection, but they do not need to be added here.
public class BookServiceTests{
private BookService bookService;
private BookDao bookDao;
private HogeDao hogeDao;
@Before
public void setUp(){
bookDao = Mockito.mock(BookDao.class);
hogeDao = Mockito.mock(HogeDao.class);
bookService = new BookService(bookDao, hogeDao);
}
@Test
public void Being able to get books with the specified bookId(){
// setUp
var expected = new Book(1, "Book name");
when(bookDao.findById(1)).thenReturn(expected);
var bookService = new BookService();
// execute
var actual = bookService.selectById(1);
// verify
assertThat(...Omitted below
}
}
Since SpringBoot related annotations are not added, the test runs smoothly.
You don't have to worry about errors like Failed to create application context
or" DB ... "and you can test without stress: smile:
If it is troublesome to mock by yourself with the method with @Before
, you can easily mock by adding @Mock
provided by Mockito.
@RunWith(MockitoJUnitRunner.class)
public class BookServiceTests{
private BookService bookService;
@Mock
private BookDao bookDao;
@Mock
private HogeDao hogeDao;
@Before
public void setUp(){
bookService = new BookService(bookDao, hogeDao);
}
//Omitted below
@RunWith (MockitoJUnitRunner.class)
is required.