Probleme mit manuell einstellen ID im Spring Boot Integrationstest mit WebTestClientJava

Java-Forum
Anonymous
 Probleme mit manuell einstellen ID im Spring Boot Integrationstest mit WebTestClient

Post by Anonymous »

Problem mit manuell einstellen ID im Spring -Boot -Integrationstest mit WebTestClient
Ich schreibe einen Integrationstest für meinen Spring -Boot -Controller mit WebTestClient. Wenn ich jedoch die ID eines Mitarbeiterunternehmens manuell festgelegt habe, bevor ich sie in meinem Test speichere, schlägt der Test fehl. Wenn ich die manuelle ID -Zuordnung entferne, geht der Test weiter.

Code: Select all

@Getter
@Setter
@Entity
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(unique = true)
private String email;

private String name;
private Long salary;
}
Mitarbeiterentität

Code: Select all

@AutoConfigureWebTestClient(timeout = "100000")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Import(TestContainerConfiguration.class)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class EmployeeControllerTestIT {

@Autowired
WebTestClient webTestClient;

@Autowired
private EmployeeRepository employeeRepository;

private Employee testEmployee;
private EmployeeDto testEmployeeDto;

@BeforeEach
void setUp() {
employeeRepository.deleteAll();

Long id = 1L;
testEmployee = Employee.builder()
.id(id) // Manually setting the ID
.email("anuj@gmail.com")
.name("Anuj")
.salary(200L)
.build();

testEmployeeDto = EmployeeDto.builder()
.id(id)
.email("anuj@gmail.com")
.name("Anuj")
.salary(200L)
.build();
}

@Test
void testGetEmployeeById_success() {
Employee savedEmployee = employeeRepository.save(testEmployee); // Fails here with error - org.springframework.orm.ObjectOptimisticLockingFailureException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.springboot.testing.entities.Employee#1]

webTestClient.get()
.uri("/employees/{id}", savedEmployee.getId())
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.id").isEqualTo(savedEmployee.getId())
.jsonPath("$.email").isEqualTo(savedEmployee.getEmail());
}
}
Ausgabe
Wenn ich die ID manuell festgelegt habe, wird die IDEERePository.save (TestAmlee) fehlschlägt. < Br /> Wenn ich die manuelle ID -Zuordnung entferne, wird der Test übergeht. Br /> Frage < /strong> < /p>
Warum setzt die ID manuell ein folgen? Wie kann ich dieses Problem debuggen oder beheben? p>
@generatedValue (Strategy = GenerationType.Identity) wird für die ID -Erzeugung verwendet. < /p>
Ich verwende TestContainer zum Integrationstest. > Was könnte dieses Problem verursachen und was wäre der beste Ansatz, um es zu beheben?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post