Cannot find @SpringBootConfiguration when executing JpaTest

I am new to frameworks (just passed class) and this is the first time I've used Spring Boot.

I am trying to run a simple Junit test to check if my CrudRepositories really work.

The error I keep getting:

Cannot find @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest (classes = ...) with your java.lang.IllegalStateException test

Spring boot not configuring on its own?

My test class:

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

My Spring Boot application starter:

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

My repository:

 public interface AccountRepository extends CrudRepository<Account,Long> { public Account findByUsername(String username); } } 
+155
java spring spring-boot spring-data junit
Aug 22 '16 at 16:30
source share
9 answers

Indeed, Spring Boot is really customizable for the most part. You can probably get rid of the large amount of code that you published, especially in Application .

I would like you to include the package names of all your classes, or at least the ones that were for Application and JpaTest . The point with @DataJpaTest and several other annotations is that they look for the @SpringBootConfiguration annotation in the current package, and if they cannot find it there, they cross the package hierarchy until they find it.

For example, if the full name for your test class was com.example.test.JpaTest and one for your application was com.example.Application , then your test class could find @SpringBootApplication (in this case @SpringBootConfiguration ).

If the application was in a different branch of the package hierarchy, however, like com.example.application.Application , it would not find it.

Example

Consider the following Maven project:

 my-test-project +--pom.xml +--src +--main +--com +--example +--Application.java +--test +--com +--example +--test +--JpaTest.java 

And then the following content in Application.java :

 package com.example; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 

The following are the contents of JpaTest.java :

 package com.example.test; @RunWith(SpringRunner.class) @DataJpaTest public class JpaTest { @Test public void testDummy() { } } 

Everything should work. If you create a new folder inside src/main/com/example called app and then place its Application.java inside it (and update the package declaration inside the file), running the test will give you the following error:

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

+227
Aug 22 '16 at 16:56
source share

The configuration is connected to the application class, so the following will be configured correctly:

 @SpringBootTest(classes = Application.class) 

An example from the JHipster project is here .

+85
Oct 30 '17 at 16:12
source share

It is worth checking if the package name of your main class is reorganized with the annotation @SpringBootApplication . In this case, the test kit should be in the appropriate package, otherwise it will look for it in the old package. it was a case for me.

+19
Oct 11 '17 at 8:53 on
source share

In addition to what Thomas Kåsene said, you can also add

 @SpringBootTest(classes=com.package.path.class) 

to the test annotation to indicate where it should look for another class if you don't want to reorganize your file hierarchy. This is what the error message suggests:

 Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) ... 
+7
Jun 07 '17 at 19:04 on
source share

In my case, the packages were different for the Application and Test classes.

 package com.example.abc; ... @SpringBootApplication public class ProducerApplication { 

as well as

 package com.example.abc_etc; ... @RunWith(SpringRunner.class) @SpringBootTest public class ProducerApplicationTest { 

After they agreed, the tests went right.

+3
Mar 19 '19 at 23:49
source share

The test snippet provided in Spring Boot 1.4 provided testing functionality.

For example,

@JsonTest provides a simple Jackson environment for testing json serialization and deserialization.

@WebMvcTest provides a dummy web environment, it can specify the controller class for testing and implement MockMvc in the test.

 @WebMvcTest(PostController.class) public class PostControllerMvcTest{ @Inject MockMvc mockMvc; } 

@DataJpaTest will prepare the embedded database and provide a basic JPA environment for the test.

@RestClientTest provides a REST client environment for the test, especially RestTemplateBuilder, etc.

These annotations are not made up of SpringBootTest, they are combined with a series of AutoconfigureXXX and @TypeExcludesFilter .

Take a look at @DataJpaTest .

 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @BootstrapWith(SpringBootTestContextBootstrapper.class) @OverrideAutoConfiguration(enabled = false) @TypeExcludeFilters(DataJpaTypeExcludeFilter.class) @Transactional @AutoConfigureCache @AutoConfigureDataJpa @AutoConfigureTestDatabase @AutoConfigureTestEntityManager @ImportAutoConfiguration public @interface DataJpaTest {} 

You can add the @AutoconfigureXXX annotation to override the default configuration.

 @AutoConfigureTestDatabase(replace=NONE) @DataJpaTest public class TestClass{ } 

Let's look at your problem,

  1. Do not mix @DataJpaTest and @SpringBootTest , as mentioned above, @DataJpaTest will create the configuration in its own way (for example, by default it will try to prepare the embedded H2 instead) from the inheritance of the application configuration. @DataJpaTest is for a test slice .
  2. If you want to configure @DataJpaTest configuration, read this @DataJpaTest official blog post on this topic (a little tedious).
  3. Split configurations in Application into smaller configurations with features such as WebConfig , DataJpaConfig , etc. A fully functional configuration (mixed network, data, security, etc.) also led to the failure of tests based on test slices . Check the test samples in my sample .
+2
Jan 13 '18 at 4:07
source share

It works for me

the package name of the above test class changes to the same as the package name of the regular class.

change to that

+2
Jul 02 '19 at 1:57
source share

I think the best solution to this problem is to align the folder structure of the tests with the folder structure of the application.

I had the same problem that was caused by duplicating my project from a project with a different folder structure.

if your test project and application project have the same structure, you will not need to add any special annotations to your test classes, and everything will work as is.

0
Nov 13 '18 at 2:36
source share
 import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @DataJpaTest @SpringBootTest @AutoConfigureWebMvc public class RepoTest { @Autowired private ThingShiftDetailsRepository thingShiftDetailsRepo; @Test public void findThingShiftDetails() { ShiftDetails details = new ShiftDetails(); details.setThingId(1); thingShiftDetailsRepo.save(details); ShiftDetails dbDetails = thingShiftDetailsRepo.findByThingId(1); System.out.println(dbDetails); } } 

Above annotations worked well for me. I am using spring boot with jpa.

-one
Oct 17 '18 at 11:07
source share



All Articles