???Spring???е????????£?
???????????? ???????[ 2013/6/7 10:59:15 ] ????????
????????????л??? profile ??????? bean
?????? Spring 3.2 ???Spring ????????? @ActiveProfiles ??????????????????e??????????????????????????????????????????????????ú????????????????????????? profile ?????????? beans?????£?
?????嵥 10. Spring-db.xml
<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-3.2.xsd">
<beans profile="test">
<bean id="datasource"
>
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:hsql://localhost" />
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
</beans>
<beans profile="production">
<bean id="datasource"
>
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:hsql://localhost/prod" />
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
</beans>
<beans profile="test??production">
<bean id="transactionManager"
>
<property name="dataSource" ref="datasource"></property>
</bean>
<bean id="initer" init-method="init">
</bean>
<bean id="accountDao" depends-on="initer">
<property name="dataSource" ref="datasource"/>
</bean>
<bean id="accountService">
</bean>
<bean id="envSetter"/>
</beans>
</beans>
???????????壬?????????
?????? ?? XML ????????????? Spring 3.2 ?? beans ???壬?????? Spring 3.2+ ???????? profile ?????
?????? ?? <beans> ????????????? <beans> ???壬???? profile ?????????????У?????????????? datasource????????? test profile????????? production profile???????????????????????м??? test profile??????? production ???????
?????? ?????漲?????Щ???????? profile ?? beans???? <beans profile=”test??production”> ?????????????Щ bean ????壬?????Щ bean ?????? profile ?ж????????
?????嵥 11. AccountServiceTest.Java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/config/Spring-db.xml")
@Transactional
@ActiveProfiles("test")
public class AccountServiceTest {
...
}
???????????? @ActiveProfiles?????????????????? profile?????????????????????????Щ????? profile ?ж???? bean ?????
?????? TestNG ?????
????Spring 2.5 ????????? TestNG ????????????????
?????? ?????? TestNG ???????? Spring ????????AbstractTransactionalTestNGSpringContextTests ???? AbstractTestNGSpringContextTests?????????? TestNG ???????????????? applicationContext ?????????
?????? ????? Spring ???????????????? @TestExecutionListeners ?????????????????????????
????→ DependencyInjectionTestExecutionListener????ò???????????????????
????→ DirtiesContextTestExecutionListener????ò???????и??? applicationContext ????
????→ TransactionalTestExecutionListener????ò????????????????????????
???????????????????????? Spring ???? TestNG ?????????в????
?????嵥 12. AccountServiceTestNGTest.Java
package testng;
import static org.Junit.Assert.assertEquals;
import org.Springframework.beans.factory.annotation.Autowired;
import org.Springframework.test.context.ActiveProfiles;
import org.Springframework.test.context.ContextConfiguration;
import org.Springframework.test.context.testng.
AbstractTransactionalTestNGSpringContextTests;
import org.Springframework.transaction.annotation.Transactional;
import service.AccountService;
import domain.Account;
@ContextConfiguration("/config/Spring-db.xml")
@Transactional
@ActiveProfiles("test")
public class AccountServiceTestNGTest extends
AbstractTransactionalTestNGSpringContextTests {
@Autowired
private AccountService service;
@org.testng.annotations.Test
public void testGetAcccountById() {
Account acct = Account.getAccount(1?? "user01"?? 18?? "M");
service.insertIfNotExist(acct);
Account acct2 = service.getAccountById(1);
assertEquals(acct??acct2);
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11