使用DB的Spring Boot应用程序-使用@DirtiesContext重新创建上下文后,测试类失败

jgbarcos

我正在尝试执行Spring Boot应用程序的一组JUnit4测试类,该类包含多个Web服务并配置了一个数据库。

在每次测试后清除上下文很方便,因此我在每个测试类上都包含了@DirtiesContext批注,因为此批注的默认行为是在AFTER_CLASS上设置的。

我遇到的问题是第一个测试类运行良好,但随后的测试类总是失败。

因此,我创建了2个简单的JUnit类来尝试解决该问题。两者均等于且测试方法为空,因此始终应返回成功:

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import urlshortener2014.goldenbrown.Application;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest("server.port=0")
@DirtiesContext
public class ApplicationTests {

    @Value("${local.server.port}")
    private int port = 0;

    @Test
    public void testAlwaysOk() throws Exception {

    }
}

我已经在eclipse中并通过“ gradle test”执行了两个测试类(ApplicationTests和SameApplicationTests),并且在两种情况下,第二个及以下测试类在清除上下文后均失败了。

我怀疑该问题与应用程序的数据库有关,未正确重新创建该数据库,因为输出跟踪多次指向与数据库相关的错误。但是我不确定这是怎么发生的或为什么发生的以及如何解决它。

这是具有“等级测试”输出(正常输出,-info输出和--debug输出)的Gist:https ://gist.github.com/jgbarcos/c8b34c5c292ca1fabc1d

这是正在使用的build.gradle(仅用于测试2个简单的类):

eclipse {
   project {
      name = "UrlShortener2014.goldenBrown"
   }
}

dependencies {
   compile project(":common")
     // Provides java script libraries for static content
   compile("org.webjars:bootstrap:3.0.3")
   compile("org.webjars:jquery:2.0.3-1")
   compile 'org.apache.httpcomponents:httpclient:4.3.6'
   compile 'nl.bitwalker:UserAgentUtils:1.2.4'
   compile 'org.springframework:spring-context'
   compile 'org.springframework:spring-context-support'
   compile 'net.sf.ehcache:ehcache:2.7.4'
   compile("org.springframework.boot:spring-boot-starter-web:1.2.0.RELEASE")
   compile 'org.springframework:spring-test:4.1.4.RELEASE'
   testCompile 'junit:junit:4.10'
}

// Used for @DirtiesContext problem
test{
    scanForTestClasses = false
    // This should get only "ApplicationTests.class" and "SameApplicationTests.class"
    include "**/*ApplicationTests.class"
}

这是我创建的GitHub分支,用于再现团队项目文件夹(goldenBrown)的问题:https : //github.com/jgbarcos/UrlShortener2014/tree/debug_branch/goldenBrown

注意:项目依赖于另一个文件夹“ / common”而不是“ / goldenBrown”中称为common的另一个项目,这可能有点棘手)

希望这有助于理解问题,在此先感谢。

弗朗西斯科·J·洛佩兹·普莱瑟

您的代码正常。故障出在schema-hsqldb.sql中只需在文件开头添加以下两行:

DROP TABLE CLICK IF EXISTS;
DROP TABLE SHORTURL IF EXISTS;

这样可以确保每次重新创建数据库时,都会删除现有表。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法在 Spring Boot 应用程序中加载应用程序上下文

来自分类Dev

如何从测试应用程序上下文中排除 Spring Boot 应用程序 bean?

来自分类Dev

由于属性文件位置类型,应用程序或集成测试 Spring 上下文创建失败

来自分类Dev

在Spring Boot应用程序中禁用表重新创建

来自分类Dev

黄瓜测试Spring Boot应用程序

来自分类Dev

从Spring Boot应用程序使用DB2 Wirelistener(NoSQL)的异常

来自分类Dev

使用简单的 Spring Boot 应用程序出现错误“应用程序无法启动”

来自分类Dev

在Spring Boot应用程序中使用Coda Hale Meter

来自分类Dev

使用-jar运行Spring Boot应用程序时出错

来自分类Dev

使用Groovy for JDBC的Spring Boot应用程序

来自分类Dev

在 Spring Boot 应用程序中使用 Soap 服务

来自分类常见问题

如何测试Spring-boot应用程序的主类

来自分类Dev

在Spring Boot中运行调度程序会生成Spring Boot应用程序上下文外部的进程

来自分类Dev

是否可以在 spring-boot 应用程序之外访问 spring 上下文?

来自分类Dev

重新加载或刷新测试方法中的Spring应用程序上下文?

来自分类Dev

应用程序上下文被加载两次-Spring Boot

来自分类Dev

Spring Boot sperate @Configurations用于多个应用程序上下文

来自分类Dev

错误:运行的应用程序失败-Spring boot + Boxfuse / AWS

来自分类Dev

如何测试Spring Boot应用程序的日志?

来自分类Dev

Spring Boot REST应用程序测试方法

来自分类Dev

单元测试Spring Boot应用程序端点

来自分类Dev

Spring Boot中多战应用程序的集成测试

来自分类Dev

单元测试Spring Boot应用程序服务层

来自分类Dev

如何测试Spring Boot应用程序的日志?

来自分类Dev

未运行 Spring Boot 应用程序的测试

来自分类Dev

测试 Spring Boot Camel 应用程序时出现 NullPointerException

来自分类Dev

Cucumber 测试不启动 Spring Boot 应用程序

来自分类Dev

Spring Boot 应用程序的测试如何工作

来自分类Dev

在没有Spring Boot应用程序的情况下使用Spring Boot Actuator

Related 相关文章

  1. 1

    无法在 Spring Boot 应用程序中加载应用程序上下文

  2. 2

    如何从测试应用程序上下文中排除 Spring Boot 应用程序 bean?

  3. 3

    由于属性文件位置类型,应用程序或集成测试 Spring 上下文创建失败

  4. 4

    在Spring Boot应用程序中禁用表重新创建

  5. 5

    黄瓜测试Spring Boot应用程序

  6. 6

    从Spring Boot应用程序使用DB2 Wirelistener(NoSQL)的异常

  7. 7

    使用简单的 Spring Boot 应用程序出现错误“应用程序无法启动”

  8. 8

    在Spring Boot应用程序中使用Coda Hale Meter

  9. 9

    使用-jar运行Spring Boot应用程序时出错

  10. 10

    使用Groovy for JDBC的Spring Boot应用程序

  11. 11

    在 Spring Boot 应用程序中使用 Soap 服务

  12. 12

    如何测试Spring-boot应用程序的主类

  13. 13

    在Spring Boot中运行调度程序会生成Spring Boot应用程序上下文外部的进程

  14. 14

    是否可以在 spring-boot 应用程序之外访问 spring 上下文?

  15. 15

    重新加载或刷新测试方法中的Spring应用程序上下文?

  16. 16

    应用程序上下文被加载两次-Spring Boot

  17. 17

    Spring Boot sperate @Configurations用于多个应用程序上下文

  18. 18

    错误:运行的应用程序失败-Spring boot + Boxfuse / AWS

  19. 19

    如何测试Spring Boot应用程序的日志?

  20. 20

    Spring Boot REST应用程序测试方法

  21. 21

    单元测试Spring Boot应用程序端点

  22. 22

    Spring Boot中多战应用程序的集成测试

  23. 23

    单元测试Spring Boot应用程序服务层

  24. 24

    如何测试Spring Boot应用程序的日志?

  25. 25

    未运行 Spring Boot 应用程序的测试

  26. 26

    测试 Spring Boot Camel 应用程序时出现 NullPointerException

  27. 27

    Cucumber 测试不启动 Spring Boot 应用程序

  28. 28

    Spring Boot 应用程序的测试如何工作

  29. 29

    在没有Spring Boot应用程序的情况下使用Spring Boot Actuator

热门标签

归档