Skip to content

Instantly share code, notes, and snippets.

@keesun
Created January 18, 2012 13:36
Show Gist options
  • Save keesun/1633037 to your computer and use it in GitHub Desktop.
Save keesun/1633037 to your computer and use it in GitHub Desktop.
Spring 3.1's TestContext
package sandbox.testcontext.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Keesun Baik
*/
@Configuration
public class AppConfig {
@Bean
public String whiteship(){
return "keesun";
}
@Bean
public String toby(){
return "toby";
}
}
package sandbox.testcontext;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import sandbox.testcontext.config.AppConfig;
import javax.annotation.Resource;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
/**
* @author Keesun Baik
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class)
public class TestContext31Tests {
@Resource String whiteship;
@Resource String toby;
@Test
public void di(){
assertThat(whiteship, is("keesun"));
assertThat(toby, is("toby"));
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="whiteship" class="java.lang.String">
<constructor-arg index="0" value="Keesun"/>
</bean>
<beans profile="default">
<bean id="toby" class="java.lang.String">
<constructor-arg index="0" value="Toby"/>
</bean>
</beans>
<beans profile="korea">
<bean id="toby" class="java.lang.String">
<constructor-arg index="0" value="이일민"/>
</bean>
</beans>
</beans>
package sandbox.profile.xml;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
/**
* @author Keesun Baik
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@ActiveProfiles("korea")
public class XmlProfileTests {
@Resource String whiteship;
@Resource String toby;
@Test public void di(){
assertThat(whiteship, is("Keesun"));
assertThat(toby, is("이일민"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment