Results 1 to 3 of 3

Thread: Using the Test interface and not setting a simple value

  1. #1

    Default Using the Test interface and not setting a simple value

    Hey - I am starting to use the test framework that Spring provides instead of writing my own (which would be hackey...)

    But I think that there is something I am fundamentally missing/screwing up.

    What is happening is that I am setting a string field through Spring and it seems to not be 'sticking'. Meaning I see it come in (debugger) and then be set to the right value - then when the test runs it is null...here is the code:

    springTest/SimpleTestSuite.java

    Code:
    package springTest;
    
    import static org.junit.Assert.*;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    
    @ContextConfiguration
    public class SimpleTestSuite extends AbstractJUnit4SpringContextTests {
      private String testName = null;
    
      public void setTestName (String testName) {
        this.testName = testName;
      }
    
      @Test
      public void testSetName () throws Exception {
        assertNotNull (this.testName);
      }
    }
    springTest/SimpleTestSuite-context.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
               
        <bean id="testSuite" class="springTest.SimpleTestSuite">
            <property name="testName" value="somecrap"/>
        </bean>
        
    </beans>
    I know it is simple but it doesn't make sense to me. Oh and the assert fails, btw...

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Your test class must not be in the context, the spring context framework will wire the dependencies for you based on the beans in the context (I suggest a read of the testing chapter of the spring reference guide).

    Simply, remove the setter, put @Autowired on the string, remove the testcase from the xml and only put the String in there. RUn again and it should work.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default Worked!

    Thanks that was very helpful! I had skimmed the chapter but missed that - oops! thank you so much!

Tags for this Thread

Posting Permissions

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