我想用不同的配置文件运行maven,但似乎无法正常工作。 我为我的JPAConfiguration创建了2个不同的java类:
JPAConfiguration.class和JPAConfigurationTest.class
@Configuration
@Profile({"dev"})
@EnableTransactionManagement(proxyTargetClass = true)
@EnableJpaRepositories(basePackages = { "com.jle.athleges.model.repository", "com.jle.athleges.security.repository" })
@ComponentScan(basePackages = { "com.jle.athleges.model.services", "com.jle.athleges.security.services" })
public class JpaConfiguration {
@Bean
public DataSource dataSource() throws SQLException {
System.out.println("use dev");
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
builder.setName("dev");
return builder.setType(EmbeddedDatabaseType.H2).build();
}
@Bean
public EntityManagerFactory entityManagerFactory() throws SQLException {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("com.jle.athleges.model.entity", "com.jle.athleges.security.entity");
factory.setDataSource(dataSource());
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
public EntityManager entityManager(EntityManagerFactory entityManagerFactory) {
return entityManagerFactory.createEntityManager();
}
@Bean
public PlatformTransactionManager transactionManager() throws SQLException {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
return txManager;
}
@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
}
}
@Configuration
@Profile({"test"})
@EnableTransactionManagement(proxyTargetClass = true)
@EnableJpaRepositories(basePackages = { "com.jle.athleges.model.repository", "com.jle.athleges.security.repository" })
@ComponentScan(basePackages = { "com.jle.athleges.model.services", "com.jle.athleges.security.services" })
public class JpaConfigurationTest {
@Bean
public DataSource dataSource() throws SQLException {
System.out.println("use test");
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
builder.setName("test");
return builder.setType(EmbeddedDatabaseType.H2).build();
}
@Bean
public EntityManagerFactory entityManagerFactory() throws SQLException {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("com.jle.athleges.model.entity", "com.jle.athleges.security.entity");
factory.setDataSource(dataSource());
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
public EntityManager entityManager(EntityManagerFactory entityManagerFactory) {
return entityManagerFactory.createEntityManager();
}
@Bean
public PlatformTransactionManager transactionManager() throws SQLException {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
return txManager;
}
@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
}
}
在我的pom.xml中,我有这个:
<project>
…
<dependencies>
…
</dependencies>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
</profile>
</profiles>
<build>
<finalName>AthleGes</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<debug>true</debug>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-maven-plugin.version}</version>
<configuration>
<jettyEnvXml>src/test/resources/jetty-env.xml</jettyEnvXml>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopKey>foo</stopKey>
<stopPort>9999</stopPort>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
最后,我有一个测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles(profiles = {"test","dev"})
@ContextConfiguration(classes = { JpaConfigurationTest.class, JpaConfiguration.class, SecurityConfig.class })
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
})
public class MemberServiceImpTest {
static Logger log = LoggerFactory.getLogger(MemberServiceImpTest.class);
@Autowired
private MemberService memberService;
@Autowired
private MemberRepository repository;
@Before
public void setUp(){
repository.deleteAll();
}
@Test
public void saveMember() {
log.debug("Start saveMember");
Member a = new Member();
a.setFirstname("aaa");
a.setLastname("hhh");
a.setId(0L);
Assert.assertNotNull(memberService.save(a));
log.debug("End saveMember");
}
@Test
public void getAllMembers() {
log.debug("Start getAllMember");
long sizeBefore = repository.count();
Member a = new Member();
a.setFirstname("aaa");
a.setLastname("hhh");
a.setId(2L);
Member b = new Member();
b.setFirstname("aaa");
b.setLastname("hhh");
b.setId(1L);
memberService.save(a);
memberService.save(b);
Assert.assertEquals(memberService.getAll().size(),sizeBefore + 2);
log.debug("End getAllMember");
}
}
当我从Eclipse运行我的单元测试时,它运行正常。如果我将测试类中的配置文件从dev移动到test并从test移动到dev,它就可以了。我的意思是测试通过和显示的消息&#34;使用dev&#34;或&#34;使用测试&#34;被展示。
我想从maven(使用m2e)运行测试,我创建了这个配置:
但是当我更改配置文件时,测试始终开始。
我尝试从Maven激活配置文件 - &gt;选择Maven Profile但是我得到了相同的结果。
我想念一些东西,但我不知道是什么。我不明白。
你可以帮帮我吗?谢谢
答案 0 :(得分:13)
根据您的配置@ActiveProfiles,两个配置文件将在测试执行期间激活。如果要通过maven控制测试用例的活动弹簧配置文件,那么我建议您从测试类中删除@ActiveProfiles注释,并将spring.profiles.active指定为系统属性。您可以使用以下任一方式执行此操作:
在maven surefire插件配置中设置:
<project>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
<profiles>
<profile>
<id>dev</id>
<activation>
<property>
<name>spring.profiles.active</name>
<value>dev</value>
</property>
</activation>
</profile>
<profile>
<id>test</id>
<activation>
<property>
<name>spring.profiles.active</name>
<value>test</value>
</property>
</activation>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Dspring.profiles.active=${spring.profiles.active}</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>
您可以通过传递没有配置文件名称的spring.profiles.active参数来简单地运行maven测试。将使用属性spring.profiles.active
的值自动激活配置文件或强>
执行期间通过maven参数将其传递给surefire插件,如:
mvn -DargLine =“ - Dspring.profiles.active = dev”
答案 1 :(得分:0)
如果您在Eclipse上定期运行不同的配置文件以进行测试,则可以直接在Eclipse中选择活动配置文件而无需修改pom。
Select active profile under Maven > Select Maven Profiles...