我试图运行一个简单的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);

    }
}

当前回答

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

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

@SpringBootTest(classes=JunitBasicsApplication.class)

其他回答

Spring Boot 1.4中提供的测试片带来了面向特性的测试功能。

例如,

@JsonTest提供了一个简单的Jackson环境来测试json的序列化和反序列化。

@WebMvcTest提供了一个模拟web环境,它可以为测试指定控制器类,并在测试中注入MockMvc。

@WebMvcTest(PostController.class)
public class PostControllerMvcTest{

    @Inject MockMvc mockMvc;

}

@DataJpaTest将准备一个嵌入式数据库,并为测试提供基本的JPA环境。

@RestClientTest为测试提供REST客户端环境,尤其是RestTemplateBuilder等。

这些注释不是由SpringBootTest组成的,它们是与一系列AutoconfigureXXX和@TypeExcludesFilter注释结合在一起的。

看看@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 {}

您可以添加@AutoconfigureXXX注释来覆盖默认配置。

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

让我们看看你的问题,

Do not mix @DataJpaTest and @SpringBootTest, as said above @DataJpaTest will build the configuration in its own way(eg. by default, it will try to prepare an embedded H2 instead) from the application configuration inheritance. @DataJpaTest is designated for test slice. If you want to customize the configuration of @DataJpaTest, please read this official blog entry from Spring.io for this topic,(a little tedious). Split the configurations in Application into smaller configurations by features, such as WebConfig, DataJpaConfig etc. A full featured configuration(mixed web, data, security etc) also caused your test slice based tests to be failed. Check the test samples in my sample.

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

@SpringBootTest(classes = Application.class)

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

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

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

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

在我的例子中,Application类和Test类的包是不同的

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

and

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

在让他们同意测试正确运行之后。

检查一下是否重构了带有@SpringBootApplication注释的主类的包名是值得的。在这种情况下,测试用例应该在一个合适的包中,否则它将在旧的包中寻找它。这就是我的情况。