我有一个具有结构的多模块项目:
app/
build.gradle
settings.gradle
app-web/
src/main/java/example/
Application.java
app-repository/
src/main/java/example/
CustomerRepository.java
Customer.java
src/test/java/example/
CustomerRepositoryTest
应用类:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
与存储库
@Repository
public interface CustomerRepository extends JpaRepository<Customer, Long> {
}
和存储库测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class CustomerRepositoryTest {
@Autowired
private CustomerRepository repository;
@Test
public void shouldSave() {
// When
final Customer saved = repository.save(new Customer());
// Then
assertThat(saved.getID()).isNotNull();
}
}
现在,当我运行此测试时,出现错误:
找不到@SpringBootConfiguration,您需要在测试中使用@ContextConfiguration 或@SpringBootTest(classes=...)
当我的存储库模块中有 Application 类时,这不会出现,但我需要将它们分开。让它发挥作用的最佳方法是什么?
如果它有助于我的 Spring 依赖项是:
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("com.h2database:h2")
testCompile("org.springframework.boot:spring-boot-starter-test")
使用插件org.springframework.boot:spring-boot-gradle-plugin:1.5.8.RELEASE