我正在学习弹簧芯我对认证和使用有一些疑问profiles into JUnit 测试.
所以我知道如果我用以下方式注释一个类:
@Profile("stub")
@Repository
public class StubAccountRepository implements AccountRepository {
private Logger logger = Logger.getLogger(StubAccountRepository.class);
private Map<String, Account> accountsByCreditCard = new HashMap<String, Account>();
/**
* Creates a single test account with two beneficiaries. Also logs creation
* so we know which repository we are using.
*/
public StubAccountRepository() {
logger.info("Creating " + getClass().getSimpleName());
Account account = new Account("123456789", "Keith and Keri Donald");
account.addBeneficiary("Annabelle", Percentage.valueOf("50%"));
account.addBeneficiary("Corgan", Percentage.valueOf("50%"));
accountsByCreditCard.put("1234123412341234", account);
}
public Account findByCreditCard(String creditCardNumber) {
Account account = accountsByCreditCard.get(creditCardNumber);
if (account == null) {
throw new EmptyResultDataAccessException(1);
}
return account;
}
public void updateBeneficiaries(Account account) {
// nothing to do, everything is in memory
}
}
我宣布一个服务豆属于stub轮廓。
所以,如果我的测试班是这样的:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=TestInfrastructureConfig.class)
@ActiveProfiles("stub")
public class RewardNetworkTests {
.....................................
.....................................
.....................................
}
这意味着它将使用属于 bean 的 beansstub配置文件和没有配置文件的 bean。这是正确的还是我错过了什么?
如果改为使用会发生什么@ActiveProfiles类(其实例将是 Spring bean)上的注释我在Java配置类?
像这样的东西:
@Configuration
@Profile("jdbc-dev")
public class TestInfrastructureDevConfig {
/**
* Creates an in-memory "rewards" database populated
* with test data for fast testing
*/
@Bean
public DataSource dataSource(){
return
(new EmbeddedDatabaseBuilder())
.addScript("classpath:rewards/testdb/schema.sql")
.addScript("classpath:rewards/testdb/test-data.sql")
.build();
}
}
具体做什么?我认为这个类中配置的所有bean都属于jdbc-dev个人资料,但我不确定。你能给我更多关于这件事的信息吗?
为什么我必须使用@Profile**配置类* 上的注释而不是直接注释我的 bean?
Tnx