Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring JUnit test keeps return nullpointerexception on my service class
    primarykey
    data
    text
    <p><strong>UPDATE 25-11-2012</strong></p> <p>Hi it seems that my context files was in the wrong 'classpath' They had to be in the test classpath. I solved this by adding a folder called resources to the src/test/ folder. In here I could put my application contexts. I have also removed the @TestExecutionListeners from the test. I also needed to add some dependencies to my pom file for javax/servlet/serlvetException and such. If anyone is interested my code can be seen at <a href="https://github.com/martin-rohwedder/MROBlog/tree/issue9" rel="nofollow">Github</a>, under the branch issue9.</p> <hr> <p><strong>Question [Solved]</strong></p> <p>I have made a simple application, by using spring 3. I have made a service class which uses a DAO class to map things to and from the database, by using the simple JDBC template.</p> <p>My problem is when i am trying to make an integration test on these classes, it return a NullPointerException (See Stacktrace). It is really annoying since I can't seem to find the answer, so I hope you guys can help me out. Below I have posted all my different files.</p> <p><strong>Stacktrace</strong></p> <pre><code>Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.115 sec &lt;&lt;&lt; FAILURE! test(dk.martinrohwedder.blog.test.ArticleServiceTest) Time elapsed: 0.009 sec &lt;&lt;&lt; ERROR! java.lang.NullPointerException at dk.martinrohwedder.blog.service.ArticleService.getFiveLastArticles(ArticleService.java:30) at dk.martinrohwedder.blog.test.ArticleServiceTest.test(ArticleServiceTest.java:57) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) 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:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) 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) </code></pre> <p><strong>ArticleServiceTest</strong></p> <pre><code>package dk.martinrohwedder.blog.test; import dk.martinrohwedder.blog.domain.Article; import dk.martinrohwedder.blog.service.ArticleService; import java.util.ArrayList; import org.junit.After; import org.junit.Before; import org.junit.Test; import static org.junit.Assert.*; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; /** * * @author Martin Rohwedder */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:test-context.xml"}) @TestExecutionListeners @TransactionConfiguration(defaultRollback = true) @Transactional public class ArticleServiceTest { @Autowired(required = true) private ArticleService articleService; //private EmbeddedDatabase db; @Before public void setUp() { //EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); //db = builder.setType(EmbeddedDatabaseType.HSQL).setName("blog").addScript("classpath:create-db.sql").addScript("classpath:test-data.sql").build(); //articleService.getArticleDao().setDataSource(db); //articleService = new ArticleService(); } @After public void tearDown() { //articleService.getArticleDao().setDataSource(null); //db.shutdown(); //articleService = null; } @Test public void test() { ArrayList&lt;Article&gt; articles = articleService.getFiveLastArticles(); int number = articles.size(); assertEquals(3, number); } } </code></pre> <p><strong>ArticleService</strong></p> <pre><code>package dk.martinrohwedder.blog.service; import dk.martinrohwedder.blog.domain.Article; import dk.martinrohwedder.blog.repository.ArticleDao; import java.util.ArrayList; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * * @author Martin Rohwedder * @since 22-11-2012 * @version 1.0 */ @Service("articleService") public class ArticleService { @Autowired(required = true) private ArticleDao articleDao; public ArticleDao getArticleDao() { return articleDao; } public void setArticleDao(ArticleDao articleDao) { this.articleDao = articleDao; } public ArrayList&lt;Article&gt; getFiveLastArticles() { ArrayList&lt;Article&gt; articles = (ArrayList) articleDao.selectAllArticles(); if (articles.size() &gt; 5) { articles = (ArrayList) articles.subList(articles.size() - 6, articles.size()); } return articles; } } </code></pre> <p><strong>ArticleDao</strong></p> <pre><code>package dk.martinrohwedder.blog.repository; import dk.martinrohwedder.blog.domain.Article; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; /** * * @author Martin Rohwedder * @since 22-11-2012 * @version 1.0 */ @Repository public class ArticleDao implements IArticleDao { private JdbcTemplate jdbcTemplate; @Autowired(required = true) public void setDataSource(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } @Override public List&lt;Article&gt; selectAllArticles() { String sql = "select article_id, headline from article"; return this.jdbcTemplate.query(sql, new ArticleMapper()); } @Override public Article selectArticle(int articleId) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void insertArticle(Article article) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void updateArticle(Article article) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void deleteArticle(int articleId) { throw new UnsupportedOperationException("Not supported yet."); } /** * Article Mapper is responsible for map all rows found in a sql * statement to Article objects. */ private static final class ArticleMapper implements RowMapper&lt;Article&gt; { @Override public Article mapRow(ResultSet rs, int rowNum) throws SQLException { Article article = new Article(); article.setId(rs.getInt("article_id")); article.setHeadline(rs.getString("headline")); return article; } } } </code></pre> <p><strong>test-context.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"&gt; &lt;context:component-scan base-package="dk.martinrohwedder.blog" /&gt; &lt;mvc:annotation-driven /&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /&gt; &lt;property name="prefix" value="/views/" /&gt; &lt;property name="suffix" value=".jsp" /&gt; &lt;/bean&gt; &lt;bean id="embeddedDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt; &lt;property name="driverClassName" value="org.hsqldb.jdbc.JDBCDriver"/&gt; &lt;property name="url" value="jdbc:hsqldb:mem:blog"/&gt; &lt;property name="username" value="sa"/&gt; &lt;property name="password" value=""/&gt; &lt;/bean&gt; &lt;jdbc:embedded-database id="dataSource" type="HSQL"/&gt; &lt;jdbc:initialize-database data-source="embeddedDataSource"&gt; &lt;jdbc:script location="classpath:create-db.sql"/&gt; &lt;jdbc:script location="classpath:test-data.sql"/&gt; &lt;/jdbc:initialize-database&gt; &lt;/beans&gt; </code></pre> <p><strong>create-db.sql</strong></p> <pre><code>create database if not exists blog; use blog; drop table if exists article; create table article ( article_id int unsigned not null auto_increment, headline varchar(30) not null, primary key (article_id) ); </code></pre> <p><strong>test-data.xml</strong></p> <pre><code>insert into article (article_headline) values ("Artikel 1"); insert into article (article_headline) values ("Artikel 2"); insert into article (article_headline) values ("Artikel 3"); </code></pre> <p>Both test-context.xml, create-db.sql and test-data.sql is in the classpath since they are in the package called Other Sources (src/main/resources).</p> <p>Hopes that anyone can help me out.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload