denismp (21) [Avatar] Offline
#1
It appears that I have to delete the *Test_Roo_IntegrationTest.aj files in order for the "perform tests" to work.

Based on what I can see in the code, the files are expecting to see data in the database. On a brand new implementation, this is not the case. Is there a configuration item that I need to fix to get the test integration to not test for existing data?
ken.rimple (246) [Avatar] Offline
#2
Re: All integration tests fail.
A few things about integration tests and Roo:

1. They are @Transactional by default, so each test starts a transaction, runs the test through its' paces, then rolls back. No data stays. If you want to keep data between test runs in your own test, and clean it up yourself, you can:

- mark the testXXX() method with the @Rollback(false) annotation
- set up / tear down with @Before or @After methods for data you need to reset
- the data stays

Are you using Hibernate as the database driver? If so, the default for your schema changes in persistence.xml is to create (i.e. drop and re-create the tables every time you start up). Change that setting to 'update' - if you are in charge of your database, and it will only adjust or add columns and relationships.

(That can get messy - keep that in mind, so sometimes if it's just a sandbox you should just drop/recreate the database and/or set the setting in persistence.xml to 'create').

We cover this in a bit of depth in chapter 3-4 and also in the testing chapter a tad, but not completely so I'm outlining it here.

2. Why do you need to delete the *Test_Roo_IntegrationTest.aj files? Do you then run the roo shell to re-create them?

3. The database data is created and generated by the DataOnDemand classes and .aj files that come along with the entity integration tests. They create 10 random rows and those rows have randomized data that is supposed to match with your annotation-driven rules (@NotNull, @Max/@Min etc). If somehow you have a problem with your rules where Roo can't figure them out, it may create invalid data (see Chapter 9, section 4 for details on Roo DOD classes and in-container testing, and the 9.1 introduction for an overview).

Bottom line you can write your own tests and create the DoD classes yourself, without letting Roo scaffold them if need be. So you can do this (from the book / memory so forgive any silly typo):

[pre]
@ContextConfiguration(...)
@RunWith(SpringJUnit4ClassRunner.class)
public class DemoIntegrationTest {


@PersistenceContext
EntityManager em;

@Before
public void initData() {
StudentDataOnDemand studentDod = new StudentDataOnDemand();
Student student = studentDod.getRandomStudent();
studentId = student.getId();
CourseDataOnDemand courseDod = new CourseDataOnDemand();
Course course1 = courseDod.getRandomCourse();
course1.setListPrice(new BigDecimal("250.0"));
course course2 = courseDod.getRandomCourse();
course2.setListPRice(new BigDecimal("250.0"));
...
student1.persist();
course1.persist();
course2.persist();
course2.flush(); // force hibernate to save to database
course2.clear(); // reset your hibernate session
}

...
@Test
public void testSomeCondition() {
Course tmpcourse = em.find(course1.getId()); // get another reference to it from db
tmpcourse.setTitle("Basket Woven"); // update it
em.flush(); // should result in an update being written by making the entity dirty
em.clear(); // reset
tmpcourse = em.find(course1.getId()); // load from db now test it
Assert.assertEquals("Basket Woven", tmpcourse.getTitle());
}
}
[/pre]


Ken

Message was edited by:
ken.rimple

Message was edited by:
ken.rimple
denismp (21) [Avatar] Offline
#3
Re: All integration tests fail.
I can answer question number 2 as a yes I am using the "test integration" or --testAutomatically which seems to generate the Test_Integration_Roo.aj files. After deleting these files, the "perform tests" works. The other files(OnDemand and so forth) are still there, it is just the test integration aj files that I have deleted.

So if I change "create" to "update", will the "perform tests" work on the second invocation or will it work on the first test?

Can I delete the code the the test directory and recreate them later with the "test integration" command after I have add data to the tables to ensure the tests don't fail? The reason I ask this, is because I notice that if the OnDemand and other test files are still there, the "test integration" does not reproduce the test integration aj files.
denismp (21) [Avatar] Offline
#4
Re: All integration tests fail.
Hi Ken,

As it turns out, the problem was being created by trying to set up my project in github. It clobbers the project directory when I clone the repository from github to my project. I was careful to save the original contents of the project directory and copy the files back into the STS project directory, but it still did not work properly.

I am going to have to learn more about github to get this to work correctly.

Sorry to bother you. The tests work correctly when I recreated the project from scratch.

-Denis
jbbarquero (84) [Avatar] Offline
#5
Re: All integration tests fail.
Hello,

GIT is not that difficult. Actually, you have your local repository and GITHUB as a remote (and other ones if you want to)

Take a look to your gitignore file, just in case.

But the problem could come from the fact that the aspect files are generated by Roo (*_Roo_*.aj) by processing the @Roo* annotations.

So, once you clone or pull from github, try to open a Roo Shell first, in order to allowing Roo to take care of the files that it manages.

If you are using STS, this IDE will do this when you import the project as a maven project or if you "Update the Project..." from the Maven option (right click on the project) if you have it already imported. STS will open it's Roo Shell in both cases.

You can close the Shell once the project has been refreshed..

Finally, since the AspectJ files has to be managed with the AJDT plugin in STS, I have to clean the project from time to time.

If you're using maven directly, the aspectj-maven-plugin takes care of the aspect files in the compile phase, so there won't be problems as long as the AspectJ files exists as I said above.