Skip to content

Commit

Permalink
Add test case for error handling
Browse files Browse the repository at this point in the history
- closes gh-10
  • Loading branch information
lukashinsch committed Jun 27, 2015
1 parent 3fdd789 commit 6eec5b1
Showing 1 changed file with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package eu.hinsch.spring.propertiesdecrypter;

import org.jasypt.exceptions.EncryptionOperationNotPossibleException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.StandardEnvironment;

import java.util.Properties;

import static org.hamcrest.Matchers.isA;

/**
* Created by lh on 27/06/15.
*/
public class DecryptingPropertiesApplicationListenerTest {

@Rule
public ExpectedException exception = ExpectedException.none();

private final DecryptingPropertiesApplicationListener listener = new DecryptingPropertiesApplicationListener();

@Test
public void shouldWrapExceptionOnEncryptionError() {
// given
StandardEnvironment environment = new StandardEnvironment();
Properties props = new Properties();
props.put("propertyDecryption.password", "NOT-A-PASSWORD");
props.put("someProperty", "{encrypted}NOT-ENCRYPTED");
environment.getPropertySources().addFirst(new PropertiesPropertySource("test-env", props));

exception.expect(RuntimeException.class);
exception.expectMessage("Unable to decrypt property value '{encrypted}NOT-ENCRYPTED'");
exception.expectCause(isA(EncryptionOperationNotPossibleException.class));

ApplicationEnvironmentPreparedEvent event = new ApplicationEnvironmentPreparedEvent(new SpringApplication(), new String[0], environment);

// when
listener.onApplicationEvent(event);

// then -> exception
}
}

0 comments on commit 6eec5b1

Please sign in to comment.