在创建测试和模拟依赖项时,这三种方法之间有什么区别?
@MockBean:
@MockBean
MyService myservice;
Run Code Online (Sandbox Code Playgroud)@嘲笑:
@Mock
MyService myservice;
Run Code Online (Sandbox Code Playgroud)Mockito.mock()
MyService myservice = Mockito.mock(MyService.class);
Run Code Online (Sandbox Code Playgroud)沿着这篇 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,我仍然无法使用 …
我正在尝试创建一个使用嵌入式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设置。
我试图关注这篇文章,但是没有用。
如何测试具有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) 我是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)
谁能详细告诉我?