Announcement Announcement Module
Collapse
No announcement yet.
Using the Test interface and not setting a simple value Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      Worked!

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

      Comment

      Working...
      X