简体   繁体   中英

SpringBootTest annotation in test classes

I had an error during my jUnit test after adding a second test class.

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

...so I added (classes=...) part to the SpringBootTest annotation, and tests run fine.

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {mySecondJUnitClass.class})
public class mySecondJUnitClass{

What I didn't understand is, I didn't add that "classes" to my first test class, I only set @SpringBootTest annotation and worked fine.

@RunWith(SpringRunner.class)
@SpringBootTest
public class myFirstJUnitClass{

When and why do we need that "classes" definition? Why is not @SpringBootTest not enough?

I found the answer (by the help of JB Nizet ). My first test class was in the subpackage of the package of my main class and the second was not. So this was why it needed a classes definition.

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