我正在使用Spring的@DataJpaTest进行测试,然后将H2用作内存数据库,如此处所述.我也在使用Flyway进行制作.但是一旦测试开始,FLyway就会启动并读取SQL文件.我如何排除FlywayAutoConfiguration,并按照Spring文档中的描述保留其余内容,以便让Hibernate为我创建H2中的表?
@RunWith(SpringRunner.class)
@DataJpaTest
public class MyRepositoryTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private MyRepository triggerRepository;
}
Run Code Online (Sandbox Code Playgroud) 我有一个带有弹簧启动的Java Web应用程序
运行测试时我需要排除一些Java配置文件:
测试配置(测试运行时需要包含):
@TestConfiguration
@PropertySource("classpath:otp-test.properties")
public class TestOTPConfig { }
Run Code Online (Sandbox Code Playgroud)
生产配置(测试运行时需要排除):
@Configuration
@PropertySource("classpath:otp.properties")
public class OTPConfig { }
Run Code Online (Sandbox Code Playgroud)
测试类(使用显式配置类):
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestAMCApplicationConfig.class)
public class AuthUserServiceTest { .... }
Run Code Online (Sandbox Code Playgroud)
测试配置:
@TestConfiguration
@Import({ TestDataSourceConfig.class, TestMailConfiguration.class, TestOTPConfig.class })
@TestPropertySource("classpath:amc-test.properties")
public class TestAMCApplicationConfig extends AMCApplicationConfig { }
Run Code Online (Sandbox Code Playgroud)
也有课:
@SpringBootApplication
public class AMCApplication { }
Run Code Online (Sandbox Code Playgroud)
当测试运行时OTPConfig,我需要TestOTPConfig...
我该怎么做?
我正在使用 Spring Boot 1.4.3.RELEASE 并希望在运行测试时从扫描中排除某些组件。
@RunWith(SpringRunner.class)
@SpringBootTest
@ComponentScan(
basePackages = {"com.foobar"},
excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {AmazonKinesisRecordChecker.class, MyAmazonCredentials.class}))
public class ApplicationTests {
@Test
public void contextLoads() {
}
}
Run Code Online (Sandbox Code Playgroud)
尽管有过滤器,当我运行测试时,不需要的组件被加载并且 Spring Boot 崩溃,因为这些类需要 AWS 环境才能正常工作:
2017-01-25 16:02:49.234 ERROR 10514 --- [ main] o.s.boot.SpringApplication : Application startup failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'amazonKinesisRecordChecker' defined in file
Run Code Online (Sandbox Code Playgroud)
问题:如何使过滤器工作?
我需要在具有以下gradle依赖的项目中使用Cassandra:
compile "org.springframework.boot:spring-boot-starter-data-cassandra"
Run Code Online (Sandbox Code Playgroud)
问题是,尽管将Cassandra AutoConfiguration类标记为从单元测试中排除,但spring仍会以某种方式尝试实例化与此依赖性相关的类。
这是我的单元测试:
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
@RunWith( SpringRunner.class )
@SpringBootTest
@AutoConfigureMockMvc
@EnableAutoConfiguration( exclude = {
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration.class,
org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration.class
} )
public class HealthTest {
@Autowired
private MockMvc mockMvc;
@Test
public void contextLoads() {
Assert.assertNotNull( mockMvc );
}
@Test
public void healthControllerIsUp() throws Exception {
mockMvc.perform( MockMvcRequestBuilders.get( "/health" ) )
.andExpect( MockMvcResultMatchers.status().isOk() );
}
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误,显然是因为Spring无法实例化群集对象。
java.lang.IllegalStateException: Failed …Run Code Online (Sandbox Code Playgroud) cassandra spring-mvc-test spring-boot spring-data-cassandra spring-boot-starter
我有以下界面:
public interface MailSender {
void sender(String to, String subject,String body);
}
Run Code Online (Sandbox Code Playgroud)
有2个实现:
public class SmtpkMailSender implements MailSender {
static Log log=LogFactory.getLog(MailSender.class);
public void sender(String to, String subject,String body){
log.info("SMTP To: "+to);
log.info("SMTP Subjecy: "+subject);
log.info("SMTP body: "+body);
}
}
Run Code Online (Sandbox Code Playgroud)
第二个是:
@Primary
public class MockMailSender implements MailSender {
static Log log=LogFactory.getLog(MailSender.class);
public void sender(String to, String subject,String body){
log.info("To: "+to);
log.info("Subject: "+subject);
log.info("body: "+body);
}
}
Run Code Online (Sandbox Code Playgroud)
我将依赖项注入到控制器类中,如下所示:
@RestController
public class MailController {
@Autowired
private MailSender smtpkMailSender;
@RequestMapping("/send")
public String send(){ …Run Code Online (Sandbox Code Playgroud) 我有一个 Spring Boot 应用程序 (1.5.10.RELEASE),其中包含一个主要的 (SpringBootApplication),如下所示:
@SpringBootApplication
@Configuration
@EntityScan(basePackages = { "db.modell", "db.modell.base" })
@ComponentScan(basePackages = { "de.gui.test" })
public class SpringBootConsoleApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootConsoleApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
和两个 REST 控制器,如下所示:
@RestController
@RequestMapping("/as")
public class AController {
@Autowired
private ARepository aRepository;
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<Collection<A>> getAs() {
return new ResponseEntity<>(orgtFarbeRepository.findAll(), HttpStatus.OK);
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<A> getA(@PathVariable long id) {
A a = ARepository.findOne(id);
if (party != null) …Run Code Online (Sandbox Code Playgroud) spring ×4
java ×3
spring-boot ×3
unit-testing ×2
cassandra ×1
rest ×1
spring-mvc ×1
spring-test ×1
testing ×1