jeudi 5 mars 2015

how to test DAO Implementations methods in spring mvc with java configuration classes and no XML using jUnit

i'm working in a spring mvc project using netbeans i created a netbeans maven web project and i configured it to be a spring mvc project with java classes configuration and NO-XML, i'm trying to do a unit test of a insert DAO implementation method and my autowired DAO variable is always null


i was following this tutorial http://ift.tt/1ylXH9C


here is my MvcConfig class



@Configuration
@ComponentScan("controller")
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {




@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}

//JDBCTEMPLATE
@Bean
public DataSource getDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setUrl("jdbc:oracle:thin:@127.0.0.1:1521:XE");
dataSource.setUsername("myPC");
dataSource.setPassword("fakePassword");

return dataSource;
}

//CREATE DAO BEAN
@Bean
public UserDAO getUserDAO() {
return new UserDAOImplementation( getDataSource() );
}
}


here is my User DAO



public interface UserDAO{

public void insertUser(User user);

}


and my UserDAOImplementation



public class UserDAOImplementation implements UserDAO{

private JdbcTemplate jdbcTemplate;

public UserDAOImplementation () {}

public UserDAOImplementation (DataSource datasource) {
this.jdbcTemplate = new JdbcTemplate(datasource);
}

@Override
public void insertUser(User user) {

String sql = "INSERT INTO User"
+ "(field1, field2, field3, field4, field5, field6)"
+ " VALUES (SEQ_user.nextval, ?, ?, ?, ?, ?)";

jdbcTemplate.update(sql, user.getField1(),
user.getField2(),
user.getField3(),
user.getField4(),
user.getField5() );
}
}


and here is my test class i have commented "@RunWith(SpringJUnit4ClassRunner.class)" beacuse it gives me this error



org/junit/runners/model/MultipleFailureException
java.lang.NoClassDefFoundError
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.withAfterClasses(SpringJUnit4ClassRunner.java:187)
at org.junit.runners.ParentRunner.classBlock(ParentRunner.java:145)
at org.junit.runners.ParentRunner.run(ParentRunner.java:235)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:162)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
Caused by: java.lang.ClassNotFoundException: org.junit.runners.model.MultipleFailureException
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 16 more


this is my test class



// this is commented because it gives me errors @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=MvcConfig .class, loader=AnnotationConfigContextLoader.class)
public class UserTest{

User user;

@Autowired
private UserDAO userDAO;

@Before
public void setUp() {
System.out.println("@Before - setUp");


user = new User();

user.setField1("a");
user.setField2("a");
user.setField3("a");
user.setField4("a");
user.setField5("a");
}

@Test
public void insertUser () {

userDAO.insertUser(user);
}


the AutoWired userDAO variable is always null


i'm running the test by right clicking it and choose run file this are the imports i'm using



import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;


i'm using netbeans 8.0.2, my thoughts are that the problem is because i'm not using this @RunWith(SpringJUnit4ClassRunner.class) beacuse it gives me the errors that i mention before, but i dont know maybe i'm missing something, this insert method works fine outside the test i just wanted to test it to test some new methods in the future and i wanted to learn how to test methods with a java configuration


Aucun commentaire:

Enregistrer un commentaire