简体   繁体   中英

Spring Boot multiple module test Autowiring

I have a multiple module project with structure:

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

With Application Class:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}

With Repository

@Repository
public interface CustomerRepository extends JpaRepository<Customer, Long> {
}

And Repository Test

@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();
    }
}

Now when I run this test I get the error:

Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

This doesnt appear when I have the Application class in my repository module but I need them to be seperated out. What's the best way to get this to work?

If it helps my Spring dependencies are:

compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("com.h2database:h2")
testCompile("org.springframework.boot:spring-boot-starter-test")

Using the plugin org.springframework.boot:spring-boot-gradle-plugin:1.5.8.RELEASE

It seems package hierarchy problem. You have to include sub projects (app-web & app-repository) to resources section in Gradle. I had same issue in my maven problem and resolved by adding to resources section. I'm not good in gradle and found a link: https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_resources

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM