Java tutorial
/* * Copyright 2014 nateriver. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package be.bittich.quote.service.impl; import be.bittich.quote.config.SpringConfigForTest; import be.bittich.quote.dao.QuoteDAO; import be.bittich.quote.model.Author; import be.bittich.quote.model.Quote; import be.bittich.quote.model.User; import be.bittich.quote.service.AuthorService; import be.bittich.quote.service.CommentService; import be.bittich.quote.service.QuoteService; import be.bittich.quote.service.UserService; import static be.bittich.quote.service.impl.AuthorServiceImplTest.createAuthor; import static be.bittich.quote.service.impl.UserDetailsServiceImplTest.createUser; import java.util.Date; import java.util.List; import javax.transaction.Transactional; import static org.hamcrest.CoreMatchers.instanceOf; import org.junit.After; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextHierarchy; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * * @author nateriver */ @ContextHierarchy({ @ContextConfiguration(classes = { SpringConfigForTest.class }) }) @RunWith(SpringJUnit4ClassRunner.class) @Transactional public class QuoteServiceImplTest { @Autowired private QuoteService quoteService; @Autowired private AuthorService authorService; @Autowired private UserService userService; @Autowired private CommentService commentService; protected static Quote createQuote(Author author, User user) { Quote q1 = new Quote(); q1.setContent("izi izi"); q1.setCreatedDate(new Date()); q1.setIdauthor(author); q1.setIduser(user); q1.setVisible(Boolean.TRUE); return q1; } @Before public void setUp() { Author author = createAuthor(); authorService.saveOrUpdate(author); assertNotNull(author.getIdauthor()); User user = createUser(null); userService.insert(user); assertNotNull(user.getIduser()); Quote quote = createQuote(author, user); quoteService.saveOrUpdate(quote); assertNotNull(quote.getIdQuote()); } /** * Test of getRepository method, of class QuoteServiceImpl. */ @Test public void testGetRepository() { assertNotNull(quoteService.getRepository()); assertThat(quoteService.getRepository(), instanceOf(QuoteDAO.class)); } @Test public void findQuoteByAuthor() { Author author = createAuthor(); author = authorService.findByFirstnameLastname(author.getFirstname(), author.getLastname()); assertNotNull(author); List<Quote> listQuote = quoteService.findByAuthor(author); assertNotNull(listQuote); assertEquals(listQuote.size(), 1); } @After public void tearDown() { } }