added tests for FakeValuesService, added exception handling

This commit is contained in:
Ricky Yim
2014-04-25 14:12:54 +10:00
parent 0dffd2c2cd
commit d800a42544
5 changed files with 62 additions and 5 deletions
+6
View File
@@ -64,6 +64,12 @@
<version>0.9.9-RC1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
@@ -22,7 +22,11 @@ public class FakeValuesService {
logger.info("Using locale " + locale);
String languageCode = locale.getLanguage();
Map valuesMap = (Map) new Yaml().load(findStream(languageCode + ".yml"));
final InputStream stream = findStream(languageCode + ".yml");
if (stream == null) {
throw new LocaleDoesNotExistException(String.format("%s could not be found, does not have a corresponding yaml file", locale));
}
Map valuesMap = (Map) new Yaml().load(stream);
valuesMap = (Map) valuesMap.get(languageCode);
fakeValuesMap = (Map<String, Object>) valuesMap.get("faker");
this.randomService = randomService;
@@ -36,7 +40,6 @@ public class FakeValuesService {
return getClass().getClassLoader().getResourceAsStream(filename);
}
/**
* Fetch a random value from an array item specified by the key
*
@@ -0,0 +1,7 @@
package com.github.javafaker.service;
public class LocaleDoesNotExistException extends RuntimeException {
public LocaleDoesNotExistException(String message) {
super(message);
}
}
@@ -0,0 +1,41 @@
package com.github.javafaker.service;
import java.util.Locale;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.when;
public class FakeValuesServiceTest {
@Mock
private RandomService randomService;
@Before
public void before() {
MockitoAnnotations.initMocks(this);
when(randomService.nextInt(anyInt())).thenReturn(0);
}
@Test
public void localeShouldLoadWhenItExists() {
new FakeValuesService(new Locale("test"), randomService);
}
@Test(expected = LocaleDoesNotExistException.class)
public void localeShouldThrowException() {
new FakeValuesService(new Locale("Does not exist"), randomService);
}
@Test
public void fetchStringShouldReturnValue() {
final FakeValuesService fakeValueService = new FakeValuesService(new Locale("test"), randomService);
assertThat(fakeValueService.fetchString("property.dummy"), is("x"));
}
}
+3 -3
View File
@@ -1,4 +1,4 @@
en:
test:
faker:
abc:
def: [x, y, z]
property:
dummy: [x, y, z]