尝试按照指南在 Spring Boot 中对 JPA 层执行测试切片,但验证约束似乎没有触发。我的设置如下
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages = "com.myapp.core.dao")
@EntityScan(basePackages = {"com.myapp.core.entity"})
public class CoreConfig {
}
@Entity
@Table(name = "users")
public class Users implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@NotNull
@Size(min = 1, max = 50)
@Column(name = "username")
private String username;
...
}
public interface UsersDAO extends CrudRepository<Users, String> {
}
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace=Replace.NONE)
public …Run Code Online (Sandbox Code Playgroud) java spring integration-testing hibernate-validator spring-boot
我有一个用 c# 编写的解决方案,.netcore 2.2,测试框架是 xunit,看起来非常像这样:
-src
--controllers
--services
-test
--controllers.integrationtest
--services.integratiotest
Run Code Online (Sandbox Code Playgroud)
我们正在使用相同数据库(solutionname.test)的两个不同的 dll(controllers.integrationtest.dll 和 services.integrationtest)执行一些集成测试
现在,当我运行"dotnet test"该解决方案时,似乎两个 dll 正在尝试访问相同的资源(数据库),并且一个 dll 出现死锁。
问题是,当我运行“dotnet test”时,我想禁用 dll 的并行执行以避免死锁,所以我搜索了该内容,文档说要禁用 dll 的并行执行,您必须:
xunit.runner.json在我所做的 csproject 的根目录中添加一个并且工作正常。(这是在 Visual Studio 上运行的,因为我用另一个删除测试名称的“_”字符的功能测试了它[methodDisplay]:)
配置 xunit.runner.json 文件以始终复制或在 Visual Studio 中保留最新版本,以便复制到 bin 文件夹中(与任何 appsetings.json 文件一样)
我读过,您可以在 assemblyInfo.cs 文件中放置一个程序集属性,顺便说一句,该属性似乎已被普通的 csproj 替换,所以我有点困惑。
我想要实现的最终目标是,当 DevOps 使用 dotnet 测试时,集成测试不会因并发问题而崩溃。
这是我第一次进行单元测试/集成测试,我有一个问题。所以我开始对我的代码做单元测试,但是我有一个方法,它实际上是整个应用程序的逻辑,其中调用了多个方法,并且需要用户输入。我如何测试该方法?方法如下:
public async Task RunAsync()
{
var watch = System.Diagnostics.Stopwatch.StartNew();
var playAgain = 'y';
do
{
var attempts = 1;
var foundNumber = false;
while (attempts < 10 && foundNumber == false)
{
try
{
var inputNumber = int.Parse(GetInput());
if (inputNumber == _randomNumber)
{
foundNumber = true;
OnSuccesfulGuess(watch, attempts);
}
else if (attempts < 10)
{
OnWrongGuessWithinAttempts(inputNumber);
}
else
{
Console.WriteLine("Oops, maybe next time.");
}
}
catch (Exception ex)
{
Console.WriteLine("Please enter a number");
}
attempts++;
}
playAgain = PlayAgain(playAgain); …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 Spring Boot 应用程序的一些集成测试从 迁移RestTemplate到WebClient. 目前,测试使用自动装配的实例TestRestTemplate
@Autowired
private TestRestTemplate restTemplate;
Run Code Online (Sandbox Code Playgroud)
运行测试时,restTemplate配置为使用与服务器运行相同的基本 URL 和(随机选择的)端口。
在我的一项测试中,我登录并保存授权响应标头值以供后续 API 调用使用。我尝试将其迁移到WebClient喜欢的样子
WebClient webClient = WebClient.create()
var authResult = webClient.post()
.uri("/api/authenticate")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(new LoginDetails(username, password))
.retrieve()
.toBodilessEntity()
.block();
// save the token that was issued and use it for subsequent requests
this.authHeader = authResult.getHeaders().getFirst(HttpHeaders.AUTHORIZATION);
Run Code Online (Sandbox Code Playgroud)
但是,当我创建WebClient这样的实例时,它没有配置为使用与应用程序相同的端口。
我尝试使用依赖注入的实例TestWebClient
@Autowired
private WebTestClient webTestClient;
Run Code Online (Sandbox Code Playgroud)
这确实将 Web 客户端连接到服务器(相同的基本 URL 和端口),但是WebTestClientAPI 与WebClient. 具体来说,它似乎只允许断言响应的属性,例如,您可以断言特定的响应标头值存在,但无法保存该标头的值。
var authResult = …Run Code Online (Sandbox Code Playgroud) c# ×2
java ×2
spring ×2
spring-boot ×2
.net-core ×1
asp.net ×1
devops ×1
unit-testing ×1
xunit ×1