Skip to content

Instantly share code, notes, and snippets.

@sizovs
Last active August 29, 2015 14:16
Show Gist options
  • Save sizovs/1b6786c9a8a8e8acaa86 to your computer and use it in GitHub Desktop.
Save sizovs/1b6786c9a8a8e8acaa86 to your computer and use it in GitHub Desktop.
package lv.craftsmans;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.junit.runners.model.Statement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = RuleInjectionTest.AppConfiguration.class)
@TestExecutionListeners(RuleSupportingDependencyInjectionTestExecutionListener.class)
public class RuleInjectionTest {
@Rule
public TestRule ruleWithAutowiredDependency = new RuleWithAutowiredDependency();
@Test
public void canInjectDependenciesIntoRuleAnnotatedMembers() {
}
@ComponentScan("lv.craftsmans")
static class AppConfiguration {
@Bean
Injectable injectable() {
return new Injectable();
}
}
static class Injectable {
void something() {
System.out.println("hello");
}
}
static class RuleWithAutowiredDependency implements TestRule {
@Autowired
Injectable injectable;
@Override
public Statement apply(Statement base, Description description) {
injectable.something();
return new Statement() {
@Override
public void evaluate() throws Throwable {
base.evaluate();
}
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment