- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Cucumber 的不同属性来为我的 Spring Boot 应用程序运行集成测试。如果我使用主应用程序类中加载的默认属性,我可以让它运行。但是,当我将 @TestPropertySource 指定到配置属性的这个不同位置时,它仍然使用主 Application 类的应用程序上下文。因此,测试执行时的应用程序上下文与应用程序在服务器上运行时的应用程序上下文相同。我不想要这个。
这是一个使用 Spring Boot 1.5.18、Cucumber 4.2.2 的工作相关 Web 应用程序。
目录结构是 src/main/java ,包含我所有的 java 类和包,src/main/resources 包含 application.properties 和其他一些目录,根级文件夹包含环境日志记录和安全属性。然后我有 src/test/java 与我的 cucumber 代码和 src/test/resources 与我修改后的 application.properties 文件,我想在测试执行时使用。我还想指定不同的环境、安全性、日志记录配置属性文件以进行测试。
这是我的 ApplicationTest.Java 类,我尝试在其中使用不同的属性源。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment =
SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource( value =
{"file:${env.dir}/deploy/acceptance/config/env.properties",
"file:${env.dir}/deploy/acceptance/config/AppConfig.properties",
"file:${env.dir}/deploy/acceptance/security/auth.properties",
"classpath:application-test.properties"})
public abstract class ApplicationTest {
public ApplicationTest() {
}
这是我的 Cucumber4Test.Java 类
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources",
plugin = {"pretty", "html:target/cucumber",
"junit:target/reports/cucumber.json"}, glue = { "com.test.packages.cucumber"
}, monochrome = true)
public class CucumberTest {
}
我不确定在我遵循教程时是否遗漏了这些类(class)中的任何内容。但正如我所说,如果我没有在 ApplicationTest 类中设置任何属性源,并在 Eclipse 中将 CucumberTest.java 作为 junit 运行,或者运行 mvn clean install、mvn test 等,则 Cucumber 测试将按预期执行。
我已经尝试了很多事情来搜索这里的问题,但似乎没有什么对我有用。 Override @PropertySource with @TestPropertySource in Spring Boot
Load different application.yml in SpringBoot Test
编辑:我认为 @TestPropertySource 不起作用的原因如下:Spring 中的属性源优先级。当我在 src/test/java 中加载 cucumber 时,它会加载我指定的那些属性,但随后会在 src/main/java 文件夹中启动应用程序。这里加载了Application.java中的默认属性。 Spring 文档说最后加载的属性优先,因此应用程序启动时我的 TestPropertySource 会被覆盖。
我的工作解决方案:我想让 Cucumber 在 Jenkins 中以独立于我们的构建和部署管道的单独作业运行。但无法找到围绕我的工作标准的配置和属性的路径和目录结构的方法。所以我做了什么:
1) 将我需要的属性添加到 src/test/resources 中的类路径中。
2)现在这有点 hacky,但是 src/test/java 中的第二个 Application.java 与 @Propertysources 反射(reflect)了我想要使用的属性。
3)在 jenkins 中,我在运行 mvn 测试之前执行预构建步骤。这个 shell 只是将 src/test/java/package/with/Application.java 移动到 src/main/java/package/with/Application.java 中。这会用不同的属性覆盖通常的 Application.java 类。
4) 运行 mvn 测试
5)利润
最佳答案
这有效。
使用默认应用程序。
package cucumber.examples.spring.txn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@SpringBootApplication
@EnableWebMvc
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
还有一些application.properties
key=App
然后运行:
package cucumber.examples.spring.txn;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
public class RunCukesTest {
}
并使用此测试上下文配置
package cucumber.examples.spring.txn;
import cucumber.api.java.Before;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
@SpringBootTest
@AutoConfigureMockMvc
public class CucumberContextConfiguration {
@Value("${key}")
private String value;
@Before
public void setup_cucumber_spring_context(){
// Dummy method so cucumber will recognize this class as glue
// and use its context configuration.
System.out.println("Property was " + value);
}
}
将打印Property was App
。
将@TestPropertySource("classpath:test.properties")
添加到CucumberContextConfiguration
并创建一个包含
test.properties
文件>
key=Test
将打印Property was Test
。
关于java - 尝试加载不同的属性以进行 Spring Boot 应用程序的 Cucumber 集成测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54430684/
虽然我经常来这里寻求解决方案,但这是我的第一篇文章! 问题: 尝试运行“railsgeneratecucumber:install”时我收到错误:找不到生成器 cucumber :安装 注意:在 Wi
希望我可以清楚地解释我的问题,以使其他人可以理解,在这里,假设我有以下两种假设情况: Scenario: Filter sweets by king size and nut content Give
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 7 年前。 Improv
我在下面的 cucumber 项目中设置了 cucumber 选项: 我通过 mvn 命令行使用 -Dcucumber.options 运行测试来覆盖我的项目的选项: mvn -f "pom.xml"
我试图运行一个简单的Feature文件,但是却遇到了Exception这样的异常: 线程“主” cucumber.runtime.CucumberException中的异常:解析功能文件时出错。 这是
我只是在学习 Cucumber 并注意到如果两个完全独立的特征有两个步骤不小心措辞相同,Cucumber 只为它们建议一个步骤定义。这是否意味着步骤定义是全局的并且它们是要共享的? 例子 假设一个业务
我正在尝试配置 FactoryGirl 以使用我的 Cucumber 测试。 我在 env.rb 中添加了以下几行 require 'factory_girl' Dir.glob(File.join(
有没有办法为 Cucumber 中的某个功能定义所有场景的清理步骤?我知道Background用于定义其后的每个场景的设置步骤,但是有没有办法定义在每个场景结束时发生的类似事情? 最佳答案 您可以在
Cucumber-jvm javadocs指出胶水元素的目的是指定stepdefinitions和hook的位置。但是,这似乎对我不起作用。可以说我在目录a中有我的功能,在步骤b中有我的步骤定义。然后
我必须测试一个消息路由应用程序,其功能大致如下:- 消息发送到应用程序- 应用程序检查消息- 根据消息内容,消息被转发到某处 绝大多数测试用例几乎相同;生成特定类型的消息,将其加载到系统中,等待几秒钟
在进行 BDD 时,Cucumber 似乎是指定行为的默认工具,但是 Cucumber 网站和文章看起来有点过时,而且不太活跃。 有哪些替代方案? 最佳答案 一个非常好的库,具有 Eclipse 的高
我有一个Cucumber表中,其中一个字段是一个日期,我想用今天的日期填充该日期。有没有一种方法可以做到这一点,而不必将今天的日期硬编码到表中? 基本上我想在表中输入Time.now.strftime
我有一个步骤定义,我希望其中有一个可选参数。我相信两次调用此步骤的示例比其他任何内容都更好地解释了我所追求的内容。 I check the favorite color count I check t
我在不同环境中执行 cucumber-jvm 场景时遇到问题。包含在场景特征文件中的数据属于一个环境。为了在不同的环境中执行场景,我需要根据要执行的环境更新特征文件中的数据。 例如,在以下场景中,我将
我有一个 cucumber 项目,我想获得项目中的所有标签,以便能够选择它们作为参数。 我找到了这个 question cucumber 可以选择获取标签,但我发现它不再起作用,然后我发现了另一个 q
@@file_name Feature: Addition 为了避免愚蠢的错误 作为一个数学白痴 我想被告知两个数字的总和 场景:两个数字相加 Given I have entered @number
Java v8.x - spring v5.x cucumber v4.2.0 我试过 temyers/cucumber-jvm-parallel-plugin 并且工作正常,但是当我进入他们的 gi
目前,尝试通过命令行窗口运行测试时,以下命令可以工作,但是自从从 info.cakes 更新到 io.cucumber 后,它似乎不再工作了? mvn test -Dcucumber.options=
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题吗? 更新问题,以便 editing this post 提供事实和引用来回答它. 关闭 4 年前。 Improve
如何使用 java 从我的功能中定义“我想要”步骤? 我的 cucumber 项目设置如下: 登录功能 Feature: User Login I want to test on "www.g
我是一名优秀的程序员,十分优秀!