我试图运行一个简单的Junit测试,看看我的CrudRepositories是否确实在工作。

我一直得到的错误是:

无法找到@SpringBootConfiguration,您需要在测试中使用@ContextConfiguration或@SpringBootTest(classes=…) java.lang.IllegalStateException

Spring Boot不自己配置吗?

我的测试类:

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

Spring Boot应用程序启动器:

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

我的库:

public interface AccountRepository extends CrudRepository<Account,Long> {

    public Account findByUsername(String username);

    }
}

当前回答

我认为这个问题的最佳解决方案是将您的测试文件夹结构与应用程序文件夹结构对齐。

我有同样的问题,这是由复制我的项目从不同的文件夹结构项目。

如果您的测试项目和应用程序项目具有相同的结构,您将不需要向测试类添加任何特殊的注释,并且一切都将照常工作。

其他回答

配置附加到应用程序类,因此下面将正确设置所有内容:

@SpringBootTest(classes = Application.class)

下面是来自JHipster项目的示例。

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

上面的注释对我来说效果很好。我使用spring boot与JPA。

这是错误本身,没有回答最初的问题:

我们正在从java 8迁移到java 11。应用程序编译成功,但错误无法找到一个@SpringBootConfiguration开始出现在集成测试时,从命令行运行使用maven(从IntelliJ它工作)。

maven-failsafe-plugin似乎看不到类路径上的类,我们通过告诉failsafe插件手动包含这些类来解决这个问题:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <configuration>
                <additionalClasspathElements>
                    <additionalClasspathElement>${basedir}/target/classes</additionalClasspathElement>
                </additionalClasspathElements>
            </configuration>
            ...
        </plugin>

这对我很有用

上述测试类的包名被更改为与普通类的包名相同。

更改为这个

当所有的类都在同一个包中时,测试类就可以工作了。当我将所有java类移动到不同的包以保持正确的项目结构时,我得到了相同的错误。

我通过在测试类中提供我的主类名来解决它,如下所示。

@SpringBootTest(classes=JunitBasicsApplication.class)