简体   繁体   中英

When do I need to mention the application class in the `@SpringBootTest` annotation?

So my test class cannot find the beans if I do not mention the application test in the test class annotations

@RunWith(SpringJUnit4ClassRunner::class)
@SpringBootTest(classes=[MySpringApplication::class])
@WebAppConfiguration
class MyFooIT{...}

I've seen (and written) other tests (in other projects and in this) where I did not need to mention the class explicitly.

I'm not sure why this is happening and I'm suspecting not knowing might result in my running into issues later on.

When do you need to provide a class with the @SpringBootTest annotation, and when don't you?

There are two situations when you may have to specify the application class you want to use in the @SpringBootTest :

  1. When your test class is out of the root package of your application class (more details around the structure here )
  2. When you have more than one application class under the package of your main application. This usually happens when you have dependencies using the same package (Module A use com.example as root package, and Module B depends on Module A and also use com.example as root package)

You may check if your MyFooIT is under the hierarchy of the root package (containing MySpringApplication ), or if you have multiple application classes under the same root package.

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