Unit Test Is Not Integration Test
Unit tests are clearly defined:
“In computer programming, unit testing is a method by which individual units of source code are tested to determine if they are fit for use. A unit is the smallest testable part of an application. In procedural programming a unit may be an individual function or procedure. In object-oriented programming a unit is usually a method…” [http://en.wikipedia.org/wiki/Unit_testing]. Applying this definition to Java EE 6 context means you have to mock out the whole environment (=all classes and Java EE resources in particular). TheEntityManager
is going to be completely mocked-out as well:
class PersistentHitStoreTest extends JUnitSuite with MockitoSugar with ShouldMatchersForJUnit{
var cut:PersistentHitStore =_
@Before
def setUp: Unit = {
cut = new PersistentHitStore();
}
@Test
def persistCacheDeletesCache:Unit = {
cut.em = mock[EntityManager]
}
...
}
[sample from x-ray]
If you are using maven, see Trouble With Crippled Java EE 6 APIs in Maven Repository And The Solution and A Natural Separation of JUnit Tests in Maven (3). One of the FAQs: "How to unit test Java EE 6 applications" is extremely easy to answer: there is no difference between unit testing of Java SE and Java EE 6 applications :-). [See also page 109 "Unit Test Is Not Integration Test" in Real World Java EE Night Hacks--Dissecting the Business Tier.]
make sure you check out FunSuite and BeforeAndAfter traits as well, for me they makes tests both more natural and more expressive.. welcome to scala :)
Posted by Kristof Jozsa on June 27, 2011 at 06:55 PM CEST #