小编Dou*_*oug的帖子

@ Mock,@ MovieBean和Mockito.mock()之间的区别

在创建测试和模拟依赖项时,这三种方法之间有什么区别?

  1. @MockBean:

    @MockBean
    MyService myservice;
    
    Run Code Online (Sandbox Code Playgroud)
  2. @嘲笑:

    @Mock
    MyService myservice;
    
    Run Code Online (Sandbox Code Playgroud)
  3. Mockito.mock()

    MyService myservice = Mockito.mock(MyService.class);
    
    Run Code Online (Sandbox Code Playgroud)

java junit spring mockito spring-boot

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

React 测试库 getByLabelText 不连接多部分 aria-labelledby

沿着这篇 MDN 文章的思路,我有一个由三个不同元素的组合标记的复选框输入:一个标签、一个文本字段,然后是另一个标签。

因此,复选框输入有一个aria-labelledby属性,其值类似于label1 text-field label2

<input type="checkbox" aria-labelledby="allow-up-to num words-in-between" />
<div>
  <label id="allow-up-to">allow up to</label>
  <input id="num" type="text" value="0" />
  <label id="words-in-between">words in between</label>
</div>
Run Code Online (Sandbox Code Playgroud)

在我的测试中,我尝试根据该标签获取复选框。

getByLabelText('text of label1')我可以使用或成功获取输入getByLabelText('text of label2'),但为其提供完整的串联标签getByLabelText('text of label1 value of text-field text of label2')会导致找不到任何内容。

getByLabelText("allow up to"); // works
getByLabelText("words in between") // works
getByLabelText("allow up to 0 words in between") // does not work
Run Code Online (Sandbox Code Playgroud)

尝试getByLabelText('value of text-field')也不起作用,但即使我从 中删除文本字段aria-labelledby,我仍然无法使用 …

react-testing-library

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

H2嵌入式数据库在春季启动期间的测试期间未获取属性

我正在尝试创建一个使用嵌入式H2数据库的测试。但是我必须更改spring.datasource.url,我不能使用spring boot创建的默认值。(这是因为我必须将H2数据库的模式更改为MYSQL)

这是我的test class

@JdbcTest
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
public class DemoApplicationTests {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    DataSource dataSource;

    @Test
    public void contextLoads() {
        System.out.println(dataSource);
   }
}
Run Code Online (Sandbox Code Playgroud)

这是我的application-test.properties

 spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL
 spring.datasource.username=dbuser
 spring.datasource.password=dbpass
Run Code Online (Sandbox Code Playgroud)

我的依赖:

compile('org.springframework.boot:spring-boot-starter-batch')
runtime('com.h2database:h2')
compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version: '1.5.3.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.5.3.RELEASE'
Run Code Online (Sandbox Code Playgroud)

控制台输出:

启动嵌入式数据库:url ='jdbc:h2:mem:bfad6b71-3e2d-4a47-a32d-c76988b3c5f6; DB_CLOSE_DELAY = -1; DB_CLOSE_ON_EXIT = false',用户名='sa'

我希望网址是这样的:jdbc:h2:mem:testdb,我也希望它使用MODE=MYSQL设置。

我试图关注这篇文章,但是没有用。

java junit spring h2 spring-boot

4
推荐指数
2
解决办法
1870
查看次数

我应该在测试期间模拟Spring Cloud Config服务器属性吗?

如何测试具有spring cloud配置服务器属性的服务作为依赖项注入其中?

  1. - 我在测试期间使用new关键字创建自己的属性吗?(new ExampleProperties())
  2. 或者我是否必须使用spring并创建某种测试属性并使用配置文件来判断要使用哪些属性?
  3. 或者我应该在测试期间让spring调用spring cloud配置服务器?

我的服务如下所示:

@Service
class Testing {

    private final ExampleProperties exampleProperties

    Testing(ExampleProperties exampleProperties) {
        this.exampleProperties = exampleProperties
    }

    String methodIWantToTest() {
        return exampleProperties.test.greeting + ' bla!'
    }
}
Run Code Online (Sandbox Code Playgroud)

我的项目在启动期间调用spring cloud配置服务器来获取属性,这可以通过以下方式启用bootstrap.properties:

spring.cloud.config.uri=http://12.345.67.89:8888
Run Code Online (Sandbox Code Playgroud)

我有一个如下所示的配置:

@Component
@ConfigurationProperties
class ExampleProperties {

    private String foo
    private int bar
    private final Test test = new Test()

    //getters and setters

    static class Test {

        private String greeting

        //getters and setters
    }
}
Run Code Online (Sandbox Code Playgroud)

属性文件如下所示:

foo=hello
bar=15

test.greeting=Hello world!
Run Code Online (Sandbox Code Playgroud)

java groovy unit-testing spring-boot spring-cloud-config

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

RTL中的getByText和screen.getByText有什么区别

我是React-Testing-Library 的新手,在网上找到了几个使用view.getByText('Greeting')and的例子screen.getByText('Greeting'),就像下面的代码一样。

它们之间有什么区别吗?

    import React from 'react'
    import { render, screen } from '@testing-library/react'
    import '@testing-library/jest-dom/extend-expect'
    import { App } from "./App";
    
    test("render the correct context", ()=>{
      const view = render(<App />);
      view.getByText("Greeting");
      screen.getByText("Greeting");
    });
Run Code Online (Sandbox Code Playgroud)

谁能详细告诉我?

react-testing-library

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