Questions tagged [atunit]

Java unit tests with mocks (JMock or EasyMock) and dependency injection (Spring or Guice)

AtUnit minimizes boilerplate code in unit tests and guides test development by enforcing good practices.

  • mark exactly one field with @Unit to indicate the object under test.
  • mark fields with @Mock or @Stub to obtain mock objects
  • inject your tests, and your test subjects, using your favorite IoC container

Mock Objects Integration

AtUnit integrates with JMock or EasyMock to provide mock objects:

  • obtain a JMock context simply by declaring a field
  • annotate fields with @Mock to obtain JMock or EasyMock mock objects
  • annotate fields with @Stub to obtain a JMock or EasyMock stub object

... or you can use your own mock objects plug-in with two easy steps:

  • implement the MockFramework interface
  • annotate your tests with @MockFrameworkClass(MyMockFramework.class)

Container Integration

AtUnit integrates with Guice or Spring to take all of the work out of dependency-injected tests.

With Guice:

  • never see the Injector, never write bootstrapping boilerplate!
  • @Inject test class fields without even defining a Module
  • declaratively obtain mock objects with @Inject @Mock
  • if you need more binding flexibility, simply have your test class implement Module

With Spring:

  • annotate fields with @Bean to get them from the Spring context
  • fields annotated with @Bean which do not appear in your Spring context are added to it automatically! (This includes @Mock and @Stub fields.)
  • AtUnit looks for a Spring XML file with the same name as your test, or you can specify the location yourself with @Context("filename")
  • Most of the time, you don't even need a Spring XML file! You can easily plug in other containers in two steps:

See their web page for more info.

3 questions
2
votes
3 answers

How I get Guice and TestNg playing along, a-la-AtUnit style

I'm using TestNG for unit & integration tests, and Guice for DI. I would like to treat my test classes like other classes - instead of directly working with the injector, I would rather define fields marked with @Inject, and get these injected…
ripper234
  • 222,824
  • 274
  • 634
  • 905
1
vote
1 answer

wicket and AtUnit

I've started playing with Wicket and I've chosen Guice as dependency injection framework. Now I'm trying to learn how to write a unit test for a WebPage object. I googled a bit and I've found this post but it mentioned AtUnit so I decided to give…
kamiseq
  • 11
  • 2
0
votes
1 answer

AtUnit and guice module overrides

I would like to use Module functionalTestModule = Modules.override(new ProductionModule()).with(new TestModule()); for test cases so I can inherit all the production bindings and just override them for tests(replacing instances with mocks). …
Dean Hiller
  • 19,235
  • 25
  • 129
  • 212