You can use Arquillian for this.
Arquillian runs your tests inside a container, deploying to an application server if necessary. You can choose to use selected classes only, so this would allow you to create a replacement ABean
that implements AInterface
, and choose not to deploy the real ABean
- which would result in your replacement being used instead.
The test would look something like this:
@Singleton(name = "ABean")
class AMockBean implements AInterface {
// Mocked bean implementation
}
@RunsWith(Arquillian.class)
public class MyTests {
@Inject
private BBean bbean;
@Deployment
public Archive<?> deployment() {
return ShrinkWrap.create(JavaArchive.class)
.addClass(BBean.class)
.addClass(AMockBean.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
@Test
public void testBBean() {
bbean.whatever();
}
}
This is all untested, but it should be something along the lines of this. Depending on your environment and dependencies, it might be more difficult to configure correctly.
Arquillian is a wonderful tool, but it can be difficult to set up exactly the way you want it when starting out. Luckily there are several guides to help you through the process.