Created
September 14, 2012 06:10
-
-
Save mguymon/3720118 to your computer and use it in GitHub Desktop.
Example of using ModelFactory with a DAO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/mguymon/model-citizen | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@Transactional | |
@TransactionConfiguration( transactionManager="transactionManager",defaultRollback=true ) | |
public class CategoryDAOTest { | |
@Autowired | |
private CategoryDAO categoryDAO; | |
@Autowired | |
private ModelFactory modelFactory; | |
private Category category; | |
@Before | |
public void initCategory() throws CreateModelException { | |
category = modelFactory.createModel( Category.class ); | |
} | |
@Test | |
public void createCategory() { | |
categoryDAO.save(category); | |
assertNotNull( category.getId() ); | |
} | |
@Test | |
public void updateCategory() { | |
category.setName( "update name test" ); | |
categoryDAO.save(category); | |
category = categoryDAO.findById( category.getId() ); | |
assertEquals( "update name test", category.getName() ); | |
category.setName( "new name" ); | |
categoryDAO.save( category ); | |
category = categoryDAO.findById( category.getId() ); | |
assertEquals( "new name", category.getName() ); | |
} | |
@Test | |
public void deleteCategory() { | |
categoryDAO.save(category); | |
categoryDAO.remove( category ); | |
assertNull( categoryDAO.findById( category.getId() ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The Unit test injects the ModelFactory from Spring, using the following bean definition: