【问题标题】:Spring boot (JPA Test) : Unable to find a @SpringBootConfiguration when doing a JpaTestSpring boot(JPA 测试):执行 JpaTest 时找不到 @SpringBootConfiguration
【发布时间】:2017-08-17 08:34:14
【问题描述】:

我正在尝试运行一个简单的 Junit 测试以查看我的 JpaRepository 是否确实在工作。

我不断收到的错误是:

Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test java.lang.IllegalStateException doesn't spring boot configure itself?

我的测试班:

@RunWith(SpringRunner.class)
@DataJpaTest
public class LogConnectionTest {

    @Autowired
    private TestEntityManager entityManager;

    @Autowired
    private LogConnectionDao logConnectionDao;

    @Test
    public void ajouterTest() throws Exception{
        this.entityManager.persist(new LogConnection("Test",4));
        LogConnection logConnection = this.logConnectionDao.findOne((long)1);
        assertThat(logConnection.getClasseName().equals("Test"));
        assertThat(logConnection.getOriginalId() != 5);
    }
}

我的 Spring 启动应用程序:

@SpringBootApplication
public class UpsysmarocLibraryLogApplication {

    public static void main(String[] args) {
        SpringApplication.run(UpsysmarocLibraryLogApplication.class, args);
    }
}

我的仓库:

public interface LogConnectionDao extends JpaRepository<LogConnection,Long> {

}

【问题讨论】:

  • 你需要在你的LogConnectionTest类上使用SpringBootTest注解

标签: java spring spring-boot spring-data-jpa


【解决方案1】:

在您的测试类上使用低于类级别的注释

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {JPAConfig.class})

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {JPAConfig.class})

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value={"myJPAConfig.xml"})

@RunWith(SpringRunner.class)
@ContextConfiguration(value={"myJPAConfig.xml"})

【讨论】:

  • 什么是 JPAConfig ?
  • 必须有任何配置文件,无论是任何 java 类(基于注释的配置)或 xml 文件,您都已在其中提供了 JPA 配置。您需要将其作为 ContextConfiguration 注释的参数提及。我已经更新了我的答案
  • 能否提供JPAConfig类的例子?
【解决方案2】:

我明白了。遵循以下约定

1> 假设您的主要 spring 应用程序如下

@EnableAutoConfiguration
@SpringBootApplication
public class Application {...}

2> 现在创建一个可以在所有测试类中扩展的抽象类。如果您不需要这个,那么只需将这些配置注释直接放入您的测试类中。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
@ActiveProfiles("test")
@WebAppConfiguration
/**
 * If you have any property file to load to test uncomment below line) 
   @TestPropertySource({
   "classpath:/properties/dbConfig-test.properties",
   "classpath:/properties/unittest.properties"
   })
*/
public abstract class AbstractSpringTest{}

然后你就可以有你的测试课了

public class LogConnectionTest extends AbstractSpringTest {

/** Instance to unit test. */
@Autowired
private LogConnectionDao logConnectionDao;

@Test
public void ajouterTest() {
    final LogConnection logConnection = logConnectionDao.saveAndFlush(new LogConnection("Test",4));
    Assert.assertNotNull(logConnection);
    Assert.assertEquals("Test", logConnection.getClasseName());
    Assert.assertNotEquals(5, logConnection.getOriginalId());
}

}

【讨论】:

    猜你喜欢
    • 2017-03-06
    • 2017-09-16
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 2016-11-28
    • 2020-07-27
    • 2016-10-10
    • 2021-10-18
    相关资源
    最近更新 更多