-
1. Re: Static declaration
bmajsak Jan 8, 2013 3:45 PM (in response to roman.valina)This the way how JUnit works. Each test is invoked on the new instance of test class, therefore field is every time initialized as empty ArrayList. And when it's static... well you know for sure
Here's plain JUnit example to prove it:
{code:java}
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class CurrencyCodeTest
{
private List<String> currencyCodes = new ArrayList<String>();
@Test
public void should_store_currency_code() throws Exception
{
currencyCodes.add("USD");
assertEquals(1, currencyCodes.size());
}
@Test
public void should_have_new_list_created() throws Exception
{
assertTrue(currencyCodes.isEmpty());
}
}
{code}
If you want to seed your database for tests have a look at persistence extension or have a look at this guide which shows a bit more pragmatic (not that flexible though) approach.
On the other hand I don't really see any value in this list. You already know what should be in the database so why store it in the list?
Hope that helps!
Cheers,
Bartosz
-
2. Re: Static declaration
roman.valina Jan 9, 2013 5:31 AM (in response to bmajsak)Oh yes I forgot this basic JUnit feature. The reason why I am want to store users in list is that I have the generated IDs in my user entity and I never know what id will be generated for the entity. I wants to have the test simple when I was asking the question.
I was already triyng to implement the persistence extension tutorial but the @PersistentContext is not injected from some reason.
Why the EntityManager is not injected when I add following to my test class. (Persisten unit is injected in to the UserDao class with no problem)
@PersistenceContext(unitName="test-persistent-unit")
private EntityManager entityManager;
This is my configuration of persistent unit:
<persistence-unit name="test-persistent-unit"
transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.aspectworks.training.domain.User</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/TrainingDS" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.password" value="password" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
Thanky you for your advices.
Roman