0

Trying to call a very simple method from a junit test. I have an interface and class:

public interface CacheSampleDao {
    String sample(String a);
}

and

public class CacheSampleImpl implements CacheSampleDao {
    public String sample(String a) {
        return "1";
    }
}

This class is also a bean in my context.xml

    <bean class="com.premierinc.datascience.repo.CacheSampleImpl"/>

and a test

@Test
public class CacheSampleTest extends AbstractTest {
    @Autowired CacheSampleDaoImpl cacheSampleDaoImpl;
    @Test
    public void cacheTest() {
        String a = cacheSampleDaoImpl.sample("A");
    }
}

Why would this test be giving a Null Pointer Exception? Is there some something else configuration related that needs to be done for this or some other step that I am missing?

4
  • 1
    your test doesn't have any annotation ? Feb 15, 2016 at 21:42
  • 2
    How do you set up Spring context in the test (I thing that's what @JérémieB means) - are there any @RunWith annotations in AbstractTest? If not, how do you expect cacheSampleDaoImpl to be injected? Feb 15, 2016 at 21:43
  • CacheSampleTest has a Test annotation, I just did an edit here to show it. Adam, I do not have a @RunWith annotation, I am researching that now
    – swinters
    Feb 15, 2016 at 21:47
  • @JérémieB yea, I added that annotation and then fixed an issue with my pom and THEN did a mvn clean and its better now. I will figure spring out eventually!
    – swinters
    Feb 15, 2016 at 22:00

2 Answers 2

0

Using an interface is fine. The issue you have is because you @Autowired statement is:

 @Autowired CacheSampleDaoImpl cacheSampleDaoImpl;

But where is your DaoImpl? The bean you have created is a CacheSampleImpl, which implements the CacheSampleDao interface, not the DaoImpl.

Also, the property should be named according to the bean you created, you have no bean named cacheSampleDaoImpl or a bean of type CacheSampleDaoImpl for the autowiring to successfully resolved.

Based on the code you have shown (which excludes the CacheSampleDaoImpl) I believe what you want is this:

@Autowired CacheSampleDao cacheSampleImpl;

There is a good post on how to do this here:

Spring autowire interface

2
  • 1
    if you read the comment, you'll see the issue is simpler than that. i suspect the name to be a cut/paste to stackoverflow error ;-) Feb 15, 2016 at 21:53
  • Yea, that was a cut/paste issue, my mistake.
    – swinters
    Feb 15, 2016 at 21:59
0

For integration tests with spring you should care about inserting spring context to the test, in other case there is nothing that will care about building and injecting of the beans, that actually happen in your test. As a solution you can annotate your test with this annotations: @ContextConfiguration and @RunWith like this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/your-spring-context.xml")

You can rad more about testing in spring in spring referents documentation.

Not the answer you're looking for? Browse other questions tagged or ask your own question.