Skip to content

Commit

Permalink
Merge pull request #455 from twicksell/handleMissingFileInTestPropert…
Browse files Browse the repository at this point in the history
…yOverride

@TestPropertyOverride now throws FileNotFoundException for missing files
  • Loading branch information
twicksell authored Oct 13, 2016
2 parents 755f4a1 + 944710d commit 471742d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.netflix.archaius.test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
Expand All @@ -18,7 +20,13 @@ public Properties getPropertiesForAnnotation(TestPropertyOverride annotation) {

for (String fileName : annotation.propertyFiles()) {
try {
properties.load(this.getClass().getClassLoader().getResourceAsStream(fileName));
InputStream propFileStream = this.getClass().getClassLoader().getResourceAsStream(fileName);
if(propFileStream != null) {
properties.load(propFileStream);
}
else {
throw new FileNotFoundException(fileName);
}
} catch (IOException e) {
throw new TestConfigException("Failed to load property file from classpath", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.netflix.archaius.test;

import static org.junit.Assert.fail;

import java.lang.annotation.Annotation;

import org.junit.Test;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

public class Archaius2RuleFailureTest {

@Test(expected = TestConfigException.class)
public void testFileNotFoundExceptionThrownWhenNoPropFileFound() throws Throwable {

Archaius2TestConfig conf = new Archaius2TestConfig();

// Annotation instance pointing to non-existent prop file
Annotation testAnnotation = new TestPropertyOverride() {

@Override
public Class<? extends Annotation> annotationType() {
return TestPropertyOverride.class;
}

@Override
public String[] value() {
return null;
}

@Override
public String[] propertyFiles() {
return new String[] { "doesNotExist.properties" };
}
};

//No-op statement
Statement base = new Statement() {
@Override
public void evaluate() throws Throwable {
}
};

Statement rule = conf.apply(base, Description.createTestDescription(getClass(), "test", testAnnotation));
rule.evaluate();
fail("Should've thrown an exception");
}
}

0 comments on commit 471742d

Please sign in to comment.