java - How to test class with local connection to EJB -
i facing problem testing class has implemented local connection ejb in constructor. connection used inside constructor , sets private attributes of instantiated class.
myclass want test:
public class myclass { private string myvalue; public myclass() throws exception { mybeanlocal local = ejbfactory.getlocal(mybeanlocal.class); myvalue = local.fetchvalue(); } public void processvalue() { ... string magic should tested ... } public string getvalue() { return myvalue; } }
ejbfactory contains enhanced lookup (with caching) , can return local or remote connection (remote requires server location).
mybeanlocal interface
public interface mylocalbean { public string fetchvalue(); }
and junit class want test myclass.processvalue method:
public class myclasstest { private myclass myclass; @before public void setup() { myclass = new myclass(); } @test public void testprocessvalue() { assert.assertequals(myclass.processvalue(), "my expected value"); } }
the question how test situation when run junits in local machine (or automatic test machine hudson or jenkins) , bean runs on application server context different local one. can't touch production code, need write test.
actually don't need make mybeanlocal functional, need myvalue set.
i thinking mocking, not familiar that.
you can use jmockit mocking api (which created) such tests:
public class myclasstest { @tested myclass myclass; @test public void processvalue() { new nonstrictexpectations() { @mocked ejbfactory fac; @mocked mybeanlocal mb; { ejbfactory.getlocal(mybeanlocal.class); result = mb; mb.fetchvalue(); result = "some value"; } }; assertequals(myclass.processvalue(), "my expected value"); } }
the mocking of ejbfactory
may omitted, if still returns object in unit testing environment.
Comments
Post a Comment