Skip to content

Instantly share code, notes, and snippets.

@mguymon
Created September 14, 2012 06:10
Show Gist options
  • Save mguymon/3720118 to your computer and use it in GitHub Desktop.
Save mguymon/3720118 to your computer and use it in GitHub Desktop.
Example of using ModelFactory with a DAO
// 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() ) );
}
}
@mguymon
Copy link
Author

mguymon commented Sep 14, 2012

The Unit test injects the ModelFactory from Spring, using the following bean definition:

    <bean id="modelFactory" class="com.tobedevoured.modelcitizen.ModelFactory">
    <property name="registerBlueprintsByPackage" value="com.examplepackage.name" />
</bean>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment