Created
October 11, 2012 16:33
-
-
Save soulcutter/3873662 to your computer and use it in GitHub Desktop.
Spring ActiveProfiles injection question
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
// Question: Can you do something like this without using interface-based injection? | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ActiveProfiles("test") | |
public class FakeDownloadUtilTest extends BaseTest { | |
@Autowired | |
DownloadUtil downloadUtil; | |
@Test | |
public void testFaked() { | |
downloadUtil.downloadImage("asdf"); // returns null rather than throwing a RuntimeException | |
} | |
} | |
//------ | |
import org.springframework.stereotype.Component; | |
@Component | |
public class DownloadUtil extends DownloadUtil { | |
public String downloadImage(String url) throws Exception { | |
return null | |
} | |
} | |
//------ | |
import org.springframework.context.annotation.Profile; | |
import org.springframework.stereotype.Component; | |
@Component | |
@Profile("test") | |
public class DownloadUtilFake extends DownloadUtil { | |
@Override | |
public String downloadImage(String url) throws Exception { | |
throw new RuntimeException("Faked out!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment