Java DTO测试始终运行null
我正在尝试实现DTO测试,因为它在测试中失败 我通过实现JSON数据提出了以下解决方案 但它总是返回null,而不是序列化的JSON对象 你知道吗 或者您是否推荐其他方法来测试DTO类 AccountBranchDto.javaJava DTO测试始终运行null,java,json,unit-testing,Java,Json,Unit Testing,我正在尝试实现DTO测试,因为它在测试中失败 我通过实现JSON数据提出了以下解决方案 但它总是返回null,而不是序列化的JSON对象 你知道吗 或者您是否推荐其他方法来测试DTO类 AccountBranchDto.java package az.iba.ms.account.dto.model; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annota
package az.iba.ms.account.dto.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@Builder
@Getter
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class AccountBranchDto {
@JsonProperty("branch_number")
private String branchNumber;
@JsonProperty("branch_name")
private String branchName;
}
AccountBranchToTest.java
package az.iba.ms.account.dto;
import az.iba.ms.account.dto.model.AccountBranchDto;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters;
import org.springframework.boot.test.autoconfigure.json.JsonTest;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.text.ParseException;
@JsonTest
@RunWith(SpringRunner.class)
public class AccountBranchDtoTest {
@Autowired
private JacksonTester<AccountBranchDto> json;
private static final String BRANCH_NUMBER = "203";
private static final String BRANCH_NAME = "QARADAG FILIALI";
private static final String JSON_TO_DESERIALIZE =
"{\"branchName\":\""
+ BRANCH_NAME
+ "\",\"branchNumber\":\""
+ BRANCH_NUMBER + "}";
private AccountBranchDto accountBranchDto;
@BeforeEach
public void setup() throws ParseException {
accountBranchDto = new AccountBranchDto(BRANCH_NUMBER, BRANCH_NAME);
}
@Test
public void branchNameSerializesTest() throws IOException {
Assertions.assertThat(this.json.write(accountBranchDto))
.extractingJsonPathStringValue("@.branchName")
.isEqualTo(BRANCH_NAME);
}
@Test
public void branchNumberSerializesTest() throws IOException {
Assertions.assertThat(this.json.write(accountBranchDto))
.extractingJsonPathStringValue("@.branchNumber")
.isEqualTo(BRANCH_NUMBER);
}
}
包az.iba.ms.account.dto;
导入az.iba.ms.account.dto.model.AccountBranchDto;
导入org.assertj.core.api.Assertions;
导入org.junit.jupiter.api.beforeach;
导入org.junit.jupiter.api.Test;
导入org.junit.runner.RunWith;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters;
导入org.springframework.boot.test.autoconfigure.json.JsonTest;
导入org.springframework.boot.test.json.JacksonTester;
导入org.springframework.test.context.junit4.SpringRunner;
导入java.io.IOException;
导入java.text.ParseException;
@JsonTest
@RunWith(SpringRunner.class)
公共类AccountBranchToTest{
@自动连线
私有杰克逊特json;
专用静态最终字符串分支\u NUMBER=“203”;
私有静态最终字符串分支\u NAME=“QARADAG FILIALI”;
私有静态最终字符串JSON_TO_反序列化=
“{\“branchName\”:\”
+分行名称
+“\”,“\”分支编号“:\”
+分支机构编号+“}”;
私人帐户Branchdto AccountBranchDto;
@之前
public void setup()引发异常{
accountBranchDto=新的accountBranchDto(分支机构编号、分支机构名称);
}
@试验
public void branchnamerializestest()引发IOException{
Assertions.assertThat(this.json.write(accountBranchDto))
.extractingJsonPathStringValue(“@.branchName”)
.isEqualTo(分行名称);
}
@试验
public void branchNumberSerializesTest()引发IOException{
Assertions.assertThat(this.json.write(accountBranchDto))
.extractingJsonPathStringValue(“@.branchNumber”)
.isEqualTo(分支机构编号);
}
}