我试图运行一个简单的Junit测试,看看我的CrudRepositories是否确实在工作。
我一直得到的错误是:
无法找到@SpringBootConfiguration,您需要在测试中使用@ContextConfiguration或@SpringBootTest(classes=…)
java.lang.IllegalStateException
Spring Boot不自己配置吗?
我的测试类:
@RunWith(SpringRunner.class)
@DataJpaTest
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class JpaTest {
@Autowired
private AccountRepository repository;
@After
public void clearDb(){
repository.deleteAll();
}
@Test
public void createAccount(){
long id = 12;
Account u = new Account(id,"Tim Viz");
repository.save(u);
assertEquals(repository.findOne(id),u);
}
@Test
public void findAccountByUsername(){
long id = 12;
String username = "Tim Viz";
Account u = new Account(id,username);
repository.save(u);
assertEquals(repository.findByUsername(username),u);
}
Spring Boot应用程序启动器:
@SpringBootApplication
@EnableJpaRepositories(basePackages = {"domain.repositories"})
@ComponentScan(basePackages = {"controllers","domain"})
@EnableWebMvc
@PropertySources(value {@PropertySource("classpath:application.properties")})
@EntityScan(basePackages={"domain"})
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
}
}
我的库:
public interface AccountRepository extends CrudRepository<Account,Long> {
public Account findByUsername(String username);
}
}
Spring Boot 1.4中提供的测试片带来了面向特性的测试功能。
例如,
@JsonTest提供了一个简单的Jackson环境来测试json的序列化和反序列化。
@WebMvcTest提供了一个模拟web环境,它可以为测试指定控制器类,并在测试中注入MockMvc。
@WebMvcTest(PostController.class)
public class PostControllerMvcTest{
@Inject MockMvc mockMvc;
}
@DataJpaTest将准备一个嵌入式数据库,并为测试提供基本的JPA环境。
@RestClientTest为测试提供REST客户端环境,尤其是RestTemplateBuilder等。
这些注释不是由SpringBootTest组成的,它们是与一系列AutoconfigureXXX和@TypeExcludesFilter注释结合在一起的。
看看@DataJpaTest。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@BootstrapWith(SpringBootTestContextBootstrapper.class)
@OverrideAutoConfiguration(enabled = false)
@TypeExcludeFilters(DataJpaTypeExcludeFilter.class)
@Transactional
@AutoConfigureCache
@AutoConfigureDataJpa
@AutoConfigureTestDatabase
@AutoConfigureTestEntityManager
@ImportAutoConfiguration
public @interface DataJpaTest {}
您可以添加@AutoconfigureXXX注释来覆盖默认配置。
@AutoConfigureTestDatabase(replace=NONE)
@DataJpaTest
public class TestClass{
}
让我们看看你的问题,
Do not mix @DataJpaTest and @SpringBootTest, as said above @DataJpaTest will build the configuration in its own way(eg. by default, it will try to prepare an embedded H2 instead) from the application configuration inheritance. @DataJpaTest is designated for test slice.
If you want to customize the configuration of @DataJpaTest, please read this official blog entry from Spring.io for this topic,(a little tedious).
Split the configurations in Application into smaller configurations by features, such as WebConfig, DataJpaConfig etc. A full featured configuration(mixed web, data, security etc) also caused your test slice based tests to be failed. Check the test samples in my sample.
Spring Boot 1.4中提供的测试片带来了面向特性的测试功能。
例如,
@JsonTest提供了一个简单的Jackson环境来测试json的序列化和反序列化。
@WebMvcTest提供了一个模拟web环境,它可以为测试指定控制器类,并在测试中注入MockMvc。
@WebMvcTest(PostController.class)
public class PostControllerMvcTest{
@Inject MockMvc mockMvc;
}
@DataJpaTest将准备一个嵌入式数据库,并为测试提供基本的JPA环境。
@RestClientTest为测试提供REST客户端环境,尤其是RestTemplateBuilder等。
这些注释不是由SpringBootTest组成的,它们是与一系列AutoconfigureXXX和@TypeExcludesFilter注释结合在一起的。
看看@DataJpaTest。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@BootstrapWith(SpringBootTestContextBootstrapper.class)
@OverrideAutoConfiguration(enabled = false)
@TypeExcludeFilters(DataJpaTypeExcludeFilter.class)
@Transactional
@AutoConfigureCache
@AutoConfigureDataJpa
@AutoConfigureTestDatabase
@AutoConfigureTestEntityManager
@ImportAutoConfiguration
public @interface DataJpaTest {}
您可以添加@AutoconfigureXXX注释来覆盖默认配置。
@AutoConfigureTestDatabase(replace=NONE)
@DataJpaTest
public class TestClass{
}
让我们看看你的问题,
Do not mix @DataJpaTest and @SpringBootTest, as said above @DataJpaTest will build the configuration in its own way(eg. by default, it will try to prepare an embedded H2 instead) from the application configuration inheritance. @DataJpaTest is designated for test slice.
If you want to customize the configuration of @DataJpaTest, please read this official blog entry from Spring.io for this topic,(a little tedious).
Split the configurations in Application into smaller configurations by features, such as WebConfig, DataJpaConfig etc. A full featured configuration(mixed web, data, security etc) also caused your test slice based tests to be failed. Check the test samples in my sample.