我是框架的新手(刚刚通过该类),这是我第一次使用Spring Boot。
我正在尝试运行一个简单的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);
}
}