Injecting @AutoWired when testing

113 views
Skip to first unread message

Matt Goodwin

unread,
Nov 11, 2015, 5:45:50 PM11/11/15
to Axon Framework Users
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 :-)

Nedim Sabic

unread,
Nov 12, 2015, 10:46:46 AM11/12/15
to Axon Framework Users
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

Allard Buijze

unread,
Nov 13, 2015, 10:40:36 AM11/13/15
to Axon Framework Users
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

--
You received this message because you are subscribed to the Google Groups "Axon Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matt Goodwin

unread,
Nov 16, 2015, 2:45:06 PM11/16/15
to Axon Framework Users
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

Allard Buijze

unread,
Nov 18, 2015, 2:37:17 PM11/18/15
to Axon Framework Users
Hi,

how did you declare the "fixture" field?
Normally, you'd want to use generics in that declaration: FixtureConfiguration<MyAggregateType>. That should resolve the issue.

Cheers,

Allard

Matt Goodwin

unread,
Nov 19, 2015, 3:48:46 PM11/19/15
to axonfr...@googlegroups.com
Cool, thats fixed the warning.

Thanks :-)

--
You received this message because you are subscribed to a topic in the Google Groups "Axon Framework Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/axonframework/CGS4O124TpE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to axonframewor...@googlegroups.com.

Matt Goodwin

unread,
Nov 19, 2015, 3:49:33 PM11/19/15
to Axon Framework Users
Cool, thats fixed it! 

Thanks 
Reply all
Reply to author
Forward
0 new messages