В настоящее время я пытаюсь реализовать какое-то приложение с использованием фреймворка Axon и Spring Boot, и мне нужно написать модульные тесты для одной из саг. В этой саге, помимо функций аксона, я также использую репозиторий весенней загрузки, который мне нужно смоделировать для теста.
Проблема в том, что репозиторий не внедряется - он всегда равен нулю. Я ДЕЙСТВИТЕЛЬНО был бы признателен за любую помощь в этом вопросе.
Найдите выпущенные классы ниже:
Сага:
@Slf4j
@Saga
@Service
public class ValidationSaga {
@Autowired
private transient CommandGateway commandGateway;
private EmployeeRepository employeeRepository;
private String correlationId;
private String emp1Code;
private String emp2Code;
private String emp1Id;
private String emp2Id;
private String emp3Id;
private String emp3Code;
@StartSaga
@SagaEventHandler(associationProperty = "correlationId")
public void on(NewMatchingDocumentAggregate.MatchingSubmittedEvent event) {
log.debug(">>> HANDLING IN SAGA");
log.debug(">>> REPO: ", employeeRepository); //At this point repo is null
this.correlationId = event.getCorrelationId();
this.emp1Code= event.getEmp1Code();
this.emp2Code= event.getEmp2Code();
this.emp1Id= event.getEmp1Id();
this.emp2Id= event.getEmp2Id();
this.emp3Id= event.getEmp3Id();
this.emp3Code= event.getEmp3Code();
if(!employeeRepository.existsById(event.getEmp1Id())) {
employeeRepository.save(EmployeeEntity.builder()
.employeeCode(event.getEmp1Code())
.employeeName(null)
.isActive(true)
.removeFromRole(false)
.build());
}
if(!employeeRepository.existsById(event.getEmp2Id())) {
employeeRepository.save(EmployeeEntity.builder()
.employeeCode(event.getEmp2Code())
.employeeName(null)
.isActive(true)
.removeFromMentorRole(false)
.build());
}
log.debug(">>> > before gateway");
commandGateway.send(new NewMatchingDocumentAggregate.ApplyContextCommand(
this.correlationId, this.emp1Code, this.emp2Code, this.emp1Id, this.emp2Id,
this.emp3Id, this.emp3Code));
}
@EndSaga
@SagaEventHandler(associationProperty = "correlationId")
public void on(NewMatchingDocumentAggregate.MatchingDefinedEvent event) {
}
}
Тестовое задание:
@Slf4j
@RunWith(MockitoJUnitRunner.class)
public class ValidationSagaTest {
@Mock
private EmployeeRepository employeeRepository;
@InjectMocks
private ValidationSaga validationSaga;
private FixtureConfiguration fixture;
@Before
public void setUp() throws Exception {
fixture = new SagaTestFixture<>(ValidationSaga.class);
}
@Test
public void shouldSendApplyContextCommand_whenEmployeesExists_givenSomeEvent() {
val correlationId = "correlationId";
val emp1Code = "emp1Code ";
val emp2Code = "emp2Code ";
val emp1Id = "emp1Id ";
val emp2Id = "emp2Id ";
val emp3Id = "emp3Id ";
val emp3Code = "emp3Code ";
when(employeeRepository.existsById(emp1Id)).thenReturn(true);
when(employeeRepository.existsById(emp2Id)).thenReturn(true);
fixture.givenNoPriorActivity()
.whenAggregate(correlationId)
.publishes(new NewMatchingDocumentAggregate.MatchingSubmittedEvent(correlationId, emp1Code,
emp2Code, emp1Id, emp2Id, emp3Id, emp3Code))
.expectActiveSagas(1)
.expectDispatchedCommands(new NewMatchingDocumentAggregate.ApplyContextCommand(correlationId,
emp1Code, emp2Code, emp1Id, emp2Id, emp3Id, emp3Code));
}
transient
. Сага - это сериализованный объект в Axon, и вы определенно не хотите протягивать свойEmployeeRepository
через сериализатор. - person Steven   schedule 15.05.2018