Created
March 10, 2016 21:24
-
-
Save Se7soz/658a514f11bde261bd29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.mockito.Mock; | |
import org.mockito.runners.MockitoJUnitRunner; | |
import java.util.Arrays; | |
import java.util.List; | |
import static org.junit.Assert.*; | |
import static org.mockito.Mockito.when; | |
@RunWith(MockitoJUnitRunner.class) // This is to run with the MockitoJunitRunner instead of the default junit runner | |
public class CSVLineReaderTest { | |
@Mock // This annotation means we are not using a real CSVLineReader object but we will mock it and the mock set up will happen in the init method | |
CSVLineReader reader; | |
CSVLineParser parser; | |
@Before | |
public void setUp() { | |
parser = new CSVLineParser(reader); | |
} | |
@Test // Unit test 1 | |
public void testNoData() { | |
when(reader.readCurrentLine()).thenReturn(null); | |
List<String> ret = parser.parseCurrent(); | |
assertNotNull("Returned list shouldn't be null", ret); | |
assertTrue("Returned list should be empty", ret.isEmpty()); | |
} | |
@Test | |
public void testParseEmptyLine() { | |
when(reader.readCurrentLine()).thenReturn(""); | |
List<String> ret = parser.parseCurrent(); | |
assertNotNull("Returned list shouldn't be null", ret); | |
assertTrue("Returned list should be empty", ret.isEmpty()); | |
} | |
@Test | |
public void testParseData1() { | |
when(reader.readCurrentLine()).thenReturn("a,b,c,d"); | |
List<String> ret = parser.parseCurrent(); | |
assertNotNull("Returned list shouldn't be null", ret); | |
assertEquals("Returned list should have 4 elements", ret.size(), 4); | |
assertEquals("Returned list should be a,b,c,d", ret, Arrays.asList(new String[]{"a", "b", "c", "d"})); | |
} | |
@Test | |
public void testParseData2() { | |
when(reader.readCurrentLine()).thenReturn("a"); | |
List<String> ret = parser.parseCurrent(); | |
assertNotNull("Returned list shouldn't be null", ret); | |
assertEquals("Returned list should have 1 element", ret.size(), 1); | |
assertEquals("Returned list should be a,b,c,d", ret, Arrays.asList(new String[]{"a"})); | |
} | |
@Test | |
public void testLongWordsData() { | |
when(reader.readCurrentLine()).thenReturn("se7so,is,gamed,gedn,."); | |
List<String> ret = parser.parseCurrent(); | |
assertNotNull("Returned list shouldn't be null", ret); | |
assertEquals("Returned list should have 5 elements", ret.size(), 5); | |
assertEquals("Returned list should be se7so,is,gamed,gedn", ret, Arrays.asList(new String[]{"se7so", "is", "gamed", "gedn", "."})); | |
} | |
@Test | |
public void testOtherDelimiters() { | |
when(reader.readCurrentLine()).thenReturn("foo-bar,foo,bar---"); | |
List<String> ret = parser.parseCurrent(); | |
assertNotNull("Returned list shouldn't be null", ret); | |
assertEquals("Returned list should have 5 elements", ret.size(), 3); | |
assertEquals("Returned list should be foo-bar,foo,bar---", ret, Arrays.asList(new String[]{"foo-bar", "foo", "bar---"})); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment