0

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

Eduardo
  • 6,900
  • 17
  • 77
  • 121
  • Try moving all classes besides the `Application` (including the test) to a separate package (e.g. `src/main/java/example/subpackage`). I recall there is a problem with component scan when `@SpringBootApplication` and `@SpringBootTest` are in the same package. I think it's a know issue, cannot find the reference though – jannis Nov 03 '17 at 09:34
  • @jannis I still get the same error :( Is there a way I can tell the test how to configure it without even having an `Application` class? – Eduardo Nov 03 '17 at 09:36
  • @jannis I have a feeling `@SpringBootTest` is intended for the REST layer so maybe its to do with that – Eduardo Nov 03 '17 at 09:41
  • Swapping out the `@SpringBootTest` for `@EnableAutoConfiguration` gets the test green but I couldnt say why – Eduardo Nov 03 '17 at 10:15
  • Mentioned reference: https://stackoverflow.com/a/43517967/4494577 – jannis Nov 03 '17 at 10:55
  • @jannis Looks useful thanks, didnt realise about the package hierachy issue – Eduardo Nov 03 '17 at 11:07

1 Answers1

0

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

Kumaresh Babu N S
  • 1,648
  • 5
  • 23
  • 39