Skip to content

Instantly share code, notes, and snippets.

@arialdomartini
Created May 31, 2013 10:44
Show Gist options
  • Save arialdomartini/5684205 to your computer and use it in GitHub Desktop.
Save arialdomartini/5684205 to your computer and use it in GitHub Desktop.
Uso di jMock
package UnitTest.JE.AdvertSearchStats.Words.Iterators;
import static org.junit.Assert.*;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.Before;
import org.junit.Test;
import JE.AdvertSearchStats.Words.Iterators.ByScoreJoinedPhrasesPostgres;
import JE.AdvertSearchStats.Words.Iterators.ContainingWordsRepository;
public class ByScoreJoinedPhrasesPostgresTest {
private Mockery context;
private ContainingWordsRepository containingWordsRepository;
@Before
public void OnBeforeOneTest() {
context = new Mockery();
containingWordsRepository = context.mock(ContainingWordsRepository.class);
}
@Test
public void HasNextShouldReturnFalseIfNoMoreRecordsAreAvailable() {
context.checking(new Expectations() {{
oneOf(containingWordsRepository).getWordsForPhrase("phrase");
will(returnValue(null));
}});
ByScoreJoinedPhrasesPostgres sut = new ByScoreJoinedPhrasesPostgres(containingWordsRepository, "phrase");
Boolean hasNext = sut.HasNext();
assertFalse(hasNext);
}
@Test
public void HasNextShouldReturnTrueIfThereAreRecordsAvailable() {
context.checking(new Expectations() {{
oneOf(containingWordsRepository).getWordsForPhrase("phrase");
will(returnValue("foo|bar"));
}});
ByScoreJoinedPhrasesPostgres sut = new ByScoreJoinedPhrasesPostgres(containingWordsRepository, "phrase");
Boolean hasNext = sut.HasNext();
assertTrue(hasNext);
}
@Test
public void NextShouldReturnNullNoMoreRecordsAreAvailable() {
context.checking(new Expectations() {{
oneOf(containingWordsRepository).getWordsForPhrase("phrase");
will(returnValue(null));
}});
ByScoreJoinedPhrasesPostgres sut = new ByScoreJoinedPhrasesPostgres(containingWordsRepository, "phrase");
String next = sut.Next();
assertNull(next);
}
@Test
public void NextShouldReturnTheFirstElementRetrievedByTheRepository() {
context.checking(new Expectations() {{
oneOf(containingWordsRepository).getWordsForPhrase("phrase");
will(returnValue("foo|bar"));
}});
ByScoreJoinedPhrasesPostgres sut = new ByScoreJoinedPhrasesPostgres(containingWordsRepository, "phrase");
String next = sut.Next();
assertEquals("foo", next);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment