小编Dhe*_*rik的帖子

使用MockMvc和jsonPath()测试错误响应

我正在尝试使用以下方法测试Spring MVC控制器的错误响应

this.mockMvc
    .perform(get("/healthChecks/9999"))
    .andExpect(status().isNotFound())
    .andExpect(jsonPath('error', is("Not Found")))
    .andExpect(jsonPath('timestamp', is(notNullValue())))
    .andExpect(jsonPath('status', is(404)))
    .andExpect(jsonPath('path', is(notNullValue())))
Run Code Online (Sandbox Code Playgroud)

但测试失败,例外: java.lang.IllegalArgumentException: json can not be null or empty 在第一个jsonPath断言上.

当我卷曲这个URL时,我得到以下内容:

{
  "timestamp": 1470242437578,
  "status": 200,
  "error": "OK",
  "exception": "gov.noaa.ncei.gis.web.HealthCheckNotFoundException",
  "message": "could not find HealthCheck 9999.",
  "path": "/healthChecks/9999"
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮我理解我做错了什么吗?

spring-mvc spring-test

5
推荐指数
1
解决办法
717
查看次数

Java Selenium Webdriver 连接被拒绝

我在我的 selenium webdriver 上遇到了非常常见的连接拒绝错误。几周前正在执行相同的代码。

我一直在阅读现有的帖子,并尝试更新 geckodriver 和 FireFox,但没有成功。我可以在另一台运行相同版本的驱动程序、浏览器和库等的计算机上运行相同的代码。如何找到该机器特有的原因?错误如下。

调试 1 调试 2 调试 3

Exception in thread "main" org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:28379 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect Build info: version: 'unknown', revision: 'unknown', time: 'unknown' System info: host: 'LT9LTDRC2', ip: '10.130.3.15', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131' Driver info: driver.version: Gecko_Driver  
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:91)  
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)     
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:250)    
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236)    
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:137)  
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:191)     at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:108)     at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:137)     at seleniumPrograms.Gecko_Driver.main(Gecko_Driver.java:13) 
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:28379 …
Run Code Online (Sandbox Code Playgroud)

java selenium selenium-webdriver connection-refused geckodriver

5
推荐指数
1
解决办法
7万
查看次数

预期条件失败:等待 By.xpath 定位的元素的可见性

我正在尝试点击 alibaba.com 网站上的登录链接

这是我的测试用例:

public class TestCase {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

        String URL = "http://www.alibaba.com/";
        WebDriver driver;
        System.setProperty("webdriver.chrome.driver",
                "D:\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get(URL);
        Thread.sleep(2000);
        SignIn.SignIn_click(driver).click();
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我定位 web 元素的对象类

package PageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class SignIn {


    private static WebElement element = null;

    public static WebElement SignIn_click(WebDriver driver) {
        element = (new WebDriverWait(driver, 10)).until(ExpectedConditions
                .visibilityOfElementLocated(By
                        .xpath("//a[@data-val='ma_signin']")));
        element = driver.findElement(By
                .xpath("//a[@data-val='ma_signin']")); …
Run Code Online (Sandbox Code Playgroud)

java selenium-webdriver

5
推荐指数
1
解决办法
4万
查看次数

无法理解 Spring Boot 如何使用 maven 管理集成测试。它是否使用故障安全插件?

我试图了解 spring boot 如何管理集成测试。

基本上,在任何具有集成测试的项目中,对此负责的插件是 maven故障安全插件。默认情况下,它执行所有后缀为IT 的类。每个例子:MyServiceIT.java

嗯,这是第一个问题。我发现的大多数 Spring Boot 运行集成测试的示例(甚至在官方文档或 Spring 博客中),测试类都有后缀Test。所以,我知道它们将由 maven surefire插件运行,这对于集成测试来说是不可取的。

但混乱还没有结束。

如果目标是运行集成测试,则spring boot maven 插件文档没有说明故障安全插件。但是,如果您想跳过它们,故障安全插件会出现在文档中。

现在,我在 Spring Boot 集成测试中使用带有后缀IT的 maven 故障安全插件,在万无一失的情况下忽略它们:

<plugin>
    <!-- for unit tests -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <excludes>
            <exclude>**/*IT.java</exclude>
        </excludes>
    </configuration>
</plugin>

<plugin>
    <!--for integrations tests-->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <configuration>
        <skipITs>${skipITs}</skipITs>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

但是另一位同事Test在他的 Spring Boot 集成测试中(在另一个项目中)使用了这个后缀,并向我声称这是正确的后缀¹。

所以......我做错了什么?Spring Boot 的这个“奇怪”设置如何用于集成测试?

¹。声纳能够获得他的测试覆盖率,而我的测试则不能。我们正在使用 Cobertura maven 插件。我认为这样做的原因是他的测试运行在surefire插件上,但在故障安全插件上我需要在pom.xml上进行额外配置,也许,在Sonar上也是如此。

java spring integration-testing maven spring-boot

5
推荐指数
1
解决办法
2216
查看次数

带构造函数参数的 Mockito 模拟

我正在使用mockito 1.9.5,并想测试我在github上发布的一个类。

问题是我需要模拟该getStringFromExternalSources方法。

我的班级代码

public class MyClass {

    String a,b,c;

    public MyClass(String a, String b, String c) {

        this.a = a;
        this.b =  b;
        this.c = c;
    }

    public String  executeLogic (String d) {

        return a + b + c + d;
    }

    public String getStringFromExternalSources (){
        return "i got it from some place else";
    }

}
Run Code Online (Sandbox Code Playgroud)

当前的测试

@RunWith(MockitoJUnitRunner.class)
public class MyClassTest {


    @Test
    public void MyClassTest() {

        MyClass mc = Mockito.spy(new MyClass("a","b","c") ); …
Run Code Online (Sandbox Code Playgroud)

testing mockito

5
推荐指数
1
解决办法
3万
查看次数

有没有办法在 RabbitMQ 队列、交换器、绑定等上进行“迁移”?

我想知道是否有任何替代方法来创建/更改/删除exchangesqueues并且bindings不依赖框架(在我的例子中是Spring)来实现这一点及其限制。

问题

我经常需要更改Routing KeyQueueExchange名称,而这些框架不允许进行这种“精细”更改。因此,趋势是继续使用队列/键的原始名称,甚至原始设置(持久、DLQ 等)。将来,这最终会导致队列的组织混乱,因为您无法轻松地对其名称、配置进行适当的维护,最终无法在不同的交换机上重新组织它们等。

实际上,实现这一目标的唯一方法是手动从每个环境中删除它们,并让框架重新创建它们。或者移动临时队列的消息来执行相同的操作。

我想知道是否有任何替代方法可以控制这个问题,比如数据库迁移工具,如 Liquibase、Flyway 等。

与数据库问题做一个并行的情况,目前让 Spring 在 RabbitMQ 中创建所有内容在我看来类似于在生产数据库上保留hbm2ddlHibernate 选项。update

java migration spring rabbitmq spring-rabbit

5
推荐指数
1
解决办法
2245
查看次数

Spring在使用h2方言进行测试时正在设置mysql方言

我在 Spring 中做一些测试,我看到了一个奇怪的事情。

当我application.properties像这样设置数据源时

spring.datasource.url = jdbc:mysql://localhost:3306/test?useSSL=false
spring.datasource.username = root
spring.datasource.password = 123456
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
Run Code Online (Sandbox Code Playgroud)

@DataJpaTest用于设置 H2 数据库进行测试,spring 使用 mysql 方言而不是 H2 方言。

这是控制台的一些行

2018-08-22 14:42:53.881  INFO 852 --- [           main] beddedDataSourceBeanFactoryPostProcessor : Replacing 'dataSource' DataSource bean with embedded version
2018-08-22 14:42:53.882  INFO 852 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'dataSource' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); …
Run Code Online (Sandbox Code Playgroud)

java mysql testing spring h2

5
推荐指数
1
解决办法
1656
查看次数

使用 mysql 驱动程序设置带有 mysql 数据库的 spring-boot 项目

MySQL 连接器是 maven 依赖项的一部分,所有数据库属性如 url、用户名、密码都在 application.properties 中提到。

获取 RuntimeException,例如:

驱动com.mysql.jdbc.Driver声称不接受jdbcUrl,jdbc/mysql://10.53.235.141:3306/hms。

请帮助解决。

application.properties

 spring.datasource.url=jdbc/mysql://10.53.235.141:3306/hms
 spring.datasource.username="root"
 spring.datasource.password="password"
 spring.datasource.driverClassName=com.mysql.jdbc.Driver
 spring.jpa.database = MYSQL

[![Project setup structure][1]][1]
Run Code Online (Sandbox Code Playgroud)

控制台异常

java mysql spring-boot

5
推荐指数
2
解决办法
3977
查看次数

Mockito findByIdOrNull 问题

我有一个这样的测试:

    @Test
    fun `can execute`() {
        whenever(countryRepository.findByIdOrNull("DE")).thenReturn(germany)
        underTest.execute()
    }
Run Code Online (Sandbox Code Playgroud)

此测试失败并显示以下错误消息:

org.mockito.exceptions.misusing.WrongTypeOfReturnValue: 
Country cannot be returned by findById()
findById() should return Optional
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests.
   Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies - 
   - …
Run Code Online (Sandbox Code Playgroud)

java unit-testing jpa mockito kotlin

5
推荐指数
1
解决办法
556
查看次数

如何使用 VBA 编辑电源查询的源?

我正在尝试使用 VBA 编辑我的查询之一的源。这是我到目前为止所拥有的:

 Dim mFormula As String

 mFormula = _

 "let Source = Excel.Workbook(File.Contents(wbname), null, true) in Source"

 query1 = ActiveWorkbook.Queries.Add("LATEST", mFormula)
Run Code Online (Sandbox Code Playgroud)

wbname之前在代码中设置过。“LATEST”已经添加,而不是删除它并阅读它,我只想更改源。这可能吗?

excel vba powerquery

4
推荐指数
1
解决办法
1万
查看次数