Results 1 to 3 of 3

Thread: Problem with AbstractDependencyInjectionSpringContextTests

  1. #1
    Join Date
    Mar 2006
    Posts
    6

    Post Problem with AbstractDependencyInjectionSpringContextTests

    Hi,

    I Want to write a Junit test case for my class which is using setter injection looks like this.

    package com.service;


    public class SenderImpl implements SmsSender
    {

    private classA objA;
    private String temp;
    private classB objB;
    private classC objC;

    public classA getObjA() {
    return objA;
    }
    public void setObjA(classA objA) {
    this.objA = objA;
    }
    public classB getObjB() {
    return objB;
    }
    public void setObjB(classB objB) {
    this.objB = objB;
    }
    public classC getObjC() {
    return objC;
    }
    public void setObjC(classC objC) {
    this.objC = objC;
    }
    public String getTemp() {
    return temp;
    }
    public void setTemp(String temp) {
    this.temp = temp;
    }
    public void businessMethod(DTO Dto) {
    // Method uses this dto
    }
    }

    I defined the all these objects in applicationContext.xml file.

    My testcase looks like this

    public class SenderImplTest extends AbstractDependencyInjectionSpringContextTests
    {
    private classA objA;
    private String temp;
    private classB objB;
    private classC objC;
    SenderImpl senderImpl=null;
    public static void main(String[] args) {
    junit.textui.TestRunner.run(SmsSenderImplTest1.cla ss);
    }

    public SenderImplTest(String arg0) {
    super();
    assertNotSame(senderImpl.getObjA(),objA);
    assertEquals(senderImpl.gettemp(),temp);
    assertNotSame(senderImpl.getObjB(),objB);
    assertNotSame(senderImpl.getObjC(),objC);


    }

    protected void onSetUp() throws Exception {
    smsSenderImpl=new SmsSenderImpl();
    objA=(objA)applicationContext.getBean("beanA");
    objB=(objB)applicationContext.getBean("beanB");
    objC=(objC)applicationContext.getBean("beanC");
    temp="Result Success";
    }

    protected void onTearDown() throws Exception {
    super.tearDown();
    }

    protected String[] getConfigLocations() {
    return new String[] {"/applicationContext.xml"};

    }

    public void testSend(DTO d)
    {
    //busineess logic
    }


    }


    It is giveing Junit test failure can any one guide is what i am doing correct or not .

  2. #2
    Join Date
    Mar 2006
    Posts
    6

    Default continuos

    I am getting error as NullPonterException
    i.e I am not getting applicationContext Object is null

  3. #3
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    667

    Lightbulb Two suggestions

    Quote Originally Posted by avakali
    ...
    junit.textui.TestRunner.run(SmsSenderImplTest1.cla ss);
    ...
    1. Is the line above meant to say "SenderImplTest.class", which is the real name of your test case? Personally since JUnit 3.8.1, I never put the main() method or a "String name" constructor into my test cases, they are just not necessary any more.

    2. Is there some reason why you're extending AbstractDependencyInjectionSpringContextTests? It's a good superclass for writing functional tests, but not necessary for writing pure unit tests. If you're only trying to do the latter, then simply extend JUnit's TestCase class, and pass mock objects to the DI setters of your POJO. I use EasyMock to create the mocks, but any similar tool would do (just don't write your own mocks, they are too much work to maintain, especially if they are to have all the cool verification features that you get out of the box with EasyMock etc.).

    HTH,

    Andrew
    Andrew Swan
    "Now is the EJB of our discontent made glorious Spring"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •