Hi, I am new to Axon and using Axon 2.4.3 with Mongo as Event Sourcing Repository. I have created separate classes for each command handlers. I need to use axon test framework for unit testing . I am not able to set up Fixture configuration.

Test Class

@Autowired
private EventSourcingRepository customerEventSourcingRepository;

@Before
public void setUp() throws Exception {
fixture = Fixtures.newGivenWhenThenFixture(CustomerAggregate.class);
UpdateAddressCommandHandler commandHandler = new UpdateAddressCommandHandler();
commandHandler.setRepository(fixture.registerRepository(customerEventSourcingRepository));
fixture.registerAnnotatedCommandHandler(commandHandler);
}

}

@Test
public void testUpdateAddress() throws Exception {

Address oldaddress = new Address(“xyz”,"zyz,“zyz”);
Address newaddress =new Address(“xyz1”,"zyz1,“zyz1”);

fixture.given(new CustomerAddedEvent(“1”,“Mayuresh”, “Harsha”,“12345”, “xyz1.abc.com”, oldaddress, new Date()))
.when(new UpdateAddressCommand(“1”, newaddress))
.expectVoidReturnType()
.expectEvents(new AddressUpdatedEvent(“1”, newaddress));
}

UpdateAddressCommandHandler.java

@Component
public class UpdateAddressCommandHandler {

private static final Logger LOG = LoggerFactory.getLogger(UpdateAddressCommandHandler.class);

@Autowired
EventSourcingRepository customerEventSourcingRepository;

@CommandHandler
public void on(UpdateAddressCommand command) {
LOG.info(“Command: ‘UpdateAddressCommand’ received.”);
CustomerAggregate customerAggregate = (CustomerAggregate) customerEventSourcingRepository.load(command.getId());
customerAggregate.UpdateAddressCommand(command.getAddress());
}

public void setRepository(FixtureConfiguration registerRepository) {
this.customerEventSourcingRepository=(EventSourcingRepository) registerRepository;
}
}

Hi Mayuresh,

In my @Before I register the repository like:

commandHandler.setRepository(fixture.getRepository());

in the commandhandler the setter for the repository would be like:

setRepository(EventSourcingRepository repository)

Hi,

the fixture isn’t really meant to run in a Spring context. Instead, the Fixture sets up special implementations of messaging components that are capable of validating your expectations. The fixture will already set up an Event Sourcing Repository, for example.

Cheers,

Allard

Hi ,
My test cases are being passed, but no data is being saved when I run the test cases. Event handlers are not being automatically called with axon tests. Am I missing something in fixture configuration? What is the correct way to set/ register repository in fixture, if I am using MongoDB as event sourcing repository. Can you please send me an example which demonstrates this? Any help regarding this will be much appreciated.

Regards,
Mayuresh

HI Mayuresh,

The test fixtures are for unittests, so you can test whether a command results in the expected events etc. It is not meant for an integration test where you might want to have actual persistence to a database. Also, the CommandBus and EventBus in the fixtures are testbusses, so no complete application flow will be performed.

If you do want to run an integrationtest I suggest to use a testcalls annotated with:


@RunWith(SpringRunner.class)
@SpringBootTest(classes = {TestConfig.class, WebApplication.class})
public class IntegrationTest {

My testconfig class configures an in memory mongo client like:

@Configuration
@EnableMongoRepositories("your.package.with.repos")
public class TestConfig extends AbstractMongoConfiguration {

   @Override
   public Mongo mongo() throws Exception {
      return new Fongo(getDatabaseName()).getMongo();
   }

   @Override
   protected String getDatabaseName() {
      return "test-fongo";
   }
}

Hope that helps!

Frank

Thank you so much Frank for bring the clarity. Let me give you quick background of my code. I have seperate classes for each command handler ( except for create customer, which is defined in CustomerAggregate class.) . In the handle() , I have to load the aggregate first EventSourcingRepository.load(), and then use apply ()( which is defined in Aggregate class). My test cases are passing for addCustomer(), since the handler for the same is defined for aggregate itself. But for other update commands, test cases are failing, because when the flow of execution reaches the handler() for any update command, it gets NullPointerException, for EventSourcingRepository, when it tries to load the aggregate for that aggregate identifier. I am using MongoDB as event source repository. Can you please guide me which this concept, and what I need to change in fixture configuration for the same.

Thanks and regards,
Mayuresh.

Hi Mayuresh,

I think you’re still combining two different concepts, here. The test fixtures merely simulate infrastructure for testing purposes. So you don’t actually use mongo in them. My guess of what’s happening is, is that you are not reading from the correct repository instance. The Fixture create a repository that you should use for the tests. This repository is the one you need to inject in your Command Handler.

Cheers,

Allard

Thank you so much Allard for your valuable insight.