执行 JpaTest 时找不到@SpringBootConfiguration
我是框架的新手(刚刚通过该类),这是我第一次使用Spring Boot。
我正在尝试运行一个简单的 Junit 测试,看看我的 CrudRepositories 是否确实在工作。
我不断得到的错误是:
无法找到@SpringBootConfiguration,您需要在测试java.lang.IllegalStateException中使用@ContextConfiguration或@SpringBootTest(classes=...)
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);
}
我的弹簧靴应用程序启动器:
@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);
}
}