Injecting @AutoWired when testing

Hi,

Im new to Axon and Spring framework so bear with me, I have created a command handler which has a @AutoWired annotation in the command handler

public class BasketCommandHandler {

    @Autowired
    private Repository<Basket> repository;

    // rest of class
}

How would I inject the @Autowired property when I want to test my command handler, my current unit test is as follows:

public class BasketCommandHandlerTest {

    private FixtureConfiguration fixture;

    @Before
    public void setUp() {
        fixture = Fixtures.newGivenWhenThenFixture(Basket.class);

        // fixture.getRepository() ? where do I inject this into my command handler?
        BasketCommandHandler myCommandHandler = new BasketCommandHandler();
        fixture.registerAnnotatedCommandHandler(myCommandHandler);
    }

    // test cases below
}

Thanks :slight_smile:

Hi Matt

You will need to annotate your JUnit class with @RunWith(SpringJUnit4ClassRunner.class) and @ContextConfiguration(locations = { “file:path to your applicationContext.xml” })
so Spring can bootstrap the application context and instantiate the required beans.

Cheers

Nedim

Hi,

when injecting the command handler into your test from Spring, do make sure that the command handler uses the repository as provided by the fixture. Otherwise, the Fixture won’t be able to validate your Command Handler’s behavior.

Do note that the fixtures are designed to run in isolation. It may work when using them in combination with Spring injection/contexts, but be prepared to run into some issues.

Cheers,

Allard

Thanks for you reply, that helps. I however have an IDE warning i hope you can help me solve, the warning is as follows:

unchecked assignment ‘org.axonframework.repository.Repository’ to org.axonframework.repository.Repository<com.company.basket>’ Reason: ‘Fixture’ has raw type, so result of getRepository is erased

@Before
public void setUp() {
    fixture = Fixtures.newGivenWhenThenFixture(Basket.class);
    BasketCommandHandler basketCommandHandler = new BasketCommandHandler(fixture.getRepository());
    fixture.registerAnnotatedCommandHandler(basketCommandHandler);
}

Any ideas on how to solve this?

Thanks

Hi,

how did you declare the “fixture” field?

Normally, you’d want to use generics in that declaration: FixtureConfiguration. That should resolve the issue.

Cheers,

Allard

Cool, thats fixed the warning.

Thanks :slight_smile:

Cool, thats fixed it!

Thanks