package pl.mielecmichal.news.services.news;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import pl.mielecmichal.news.entities.news.News;
import pl.mielecmichal.news.entities.newssources.NewsSource;
import pl.mielecmichal.news.repositories.news.NewsRepository;
import static java.util.Arrays.asList;
import static org.mockito.Mockito.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class NewsServiceTest {
NewsService newsService;
NewsRepository newsRepository;
private static final String FIRST_AUTHOR = "[email protected]";
private static final String FIRST_TITLE = "First Title";
private static final String FIRST_CONTENT = "First Content Content Content";
private static final String FIRST_URL = "http://localhost/first";
private static final String SECOND_AUTHOR = "[email protected]";
private static final String SECOND_TITLE = "Second";
private static final String SECOND_CONTENT = "Second Content";
private static final String THIRD_AUTHOR = "[email protected]";
private static final String THIRD_TITLE = "Third Title";
private static final String THIRD_CONTENT = "Third Content";
private final News firstNews = firstCorrectNews();
private final News secondNews = secondCorrectNews();
private final News thirdNews = thirdCorrectNews();
private final NewsSource source = correctSource();
public NewsServiceTest() throws MalformedURLException {
}
@Before
public void setUp() throws MalformedURLException {
newsRepository = mock(NewsRepository.class);
newsService = new NewsService(newsRepository);
}
@Test
public void saveNewNewses_savedNewsesGivenAgain_shouldSaveOnlyNew() {
// given
List<News> newses = new ArrayList<>();
newses.add(firstNews);
newses.add(secondNews);
when(newsRepository.countByNewsSourceAndAuthorAndTitle(source, FIRST_AUTHOR, FIRST_TITLE)).thenReturn(0L);
when(newsRepository.countByNewsSourceAndAuthorAndTitle(source, SECOND_AUTHOR, SECOND_TITLE)).thenReturn(1L);
// when
newsService.saveNewNewses(newses);
// then
verify(newsRepository, times(1)).save(asList(firstNews));
verify(newsRepository, never()).save(newses);
}
private News firstCorrectNews() {
News news = new News();
news.setAuthor(FIRST_AUTHOR);
news.setTitle(FIRST_TITLE);
news.setContent(FIRST_CONTENT);
news.setNewsSource(source);
return news;
}
private News secondCorrectNews() {
News news = new News();
news.setAuthor(SECOND_AUTHOR);
news.setTitle(SECOND_TITLE);
news.setContent(SECOND_CONTENT);
news.setNewsSource(source);
return news;
}
private News thirdCorrectNews() {
News news = new News();
news.setAuthor(THIRD_AUTHOR);
news.setTitle(THIRD_TITLE);
news.setContent(THIRD_CONTENT);
news.setNewsSource(source);
return news;
}
private NewsSource correctSource() throws MalformedURLException {
NewsSource source = new NewsSource();
source.setUrl(new URL(FIRST_URL));
source.setUpdateTime(LocalDateTime.now());
return source;
}
}
我在調試器下進行檢查,並且countBy方法總是返回O,但參數在我的SUT中不同且正確。它看起來像Mockito 區分方法的參數。乾杯!Mockito的mock返回相同的值,儘管有不同的參數
我添加完整的源代碼來顯示常量是正確的。
你可能還需要指定什麼'FIRST_AUTHOR','SECOND_AUTHOR','FIRST_TITLE','SECOND_TITLE','source',以及如何使用的嘲弄。 – khelwood
問題已更新 –