Last active
August 29, 2015 14:06
-
-
Save thomasdarimont/0114979d6ac99a7414d2 to your computer and use it in GitHub Desktop.
How to overwrite nested dependencies for an integration test.
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
package demo.business; | |
public interface BusinessService { | |
void businessMethod(); | |
} |
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
package demo.business; | |
import org.springframework.beans.factory.annotation.Autowired; | |
public class DefaultBusinessService implements BusinessService { | |
private final Dependency dependency; | |
@Autowired | |
public DefaultBusinessService(Dependency dependency) { | |
this.dependency = dependency; | |
} | |
@Override | |
public void businessMethod() { | |
System.out.println(dependency); | |
} | |
} |
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
package demo.business; | |
public interface Dependency {} |
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
package demo.business; | |
public class DefaultDependency implements Dependency{} |
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
package demo.business; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
@Configuration | |
public class BusinessConfig { | |
@Bean | |
public BusinessService businessService(Dependency dependency) { | |
return new DefaultBusinessService(dependency); | |
} | |
@Bean | |
public Dependency dependency() { | |
return new DefaultDependency(); | |
} | |
} |
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
package demo.business; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.Import; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
@ContextConfiguration | |
@RunWith(SpringJUnit4ClassRunner.class) | |
public class BusinessServiceIT { | |
@Autowired BusinessService businessService; | |
@Import(BusinessConfig.class) | |
@Configuration | |
static class Config{ | |
//@Bean @Primary //uncomment the bean definition to get the "mock" dependency wired into the DefaultBusinessService | |
public Dependency dependency(){ | |
return new Dependency() {}; | |
} | |
} | |
@Test | |
public void test() { | |
businessService.businessMethod(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment