Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@soulcutter
Created October 11, 2012 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soulcutter/3873662 to your computer and use it in GitHub Desktop.
Save soulcutter/3873662 to your computer and use it in GitHub Desktop.
Spring ActiveProfiles injection question
// 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