Spring Bean:似乎没有接线

时间:2012-07-02 17:26:28

标签: java spring

我的wireup.xml看起来像

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <util:properties id="mongoProperties" location="file:///storage//local.properties" />

    <bean id="mongoService" class="com.business.persist.MongoService" init-method="init"></bean>
</beans>  

storage//local.properties

### === MongoDB interaction === ###
host="127.0.0.1"
port=27017
database=contract  

Bean类MongoService

@Service
public class MongoService {

    @Value("#{mongoProperties['host']}")
    private String host;

    @Value("#{mongoProperties['port']}")
    private int port;

    @Value("#{mongoProperties['database']}")
    private String database;

    private Mongo mongo;

    private static final Logger LOGGER = LoggerFactory.getLogger(MongoService.class);

    public MongoService() {}

    public void init() throws UnknownHostException {
        LOGGER.info("host=" + host + ", port=" + port + ", database=" + database);
        mongo = new Mongo(host, port);
    }

    public void putDocument(@Nonnull final DBObject document) {
        LOGGER.info("inserting document - " + document.toString());
        mongo.getDB(database).getCollection(getCollectionName(document)).insert(document, WriteConcern.SAFE);
    }

    public void putDocuments(@Nonnull final List<DBObject> documents) {
        for (final DBObject document : documents) {
            putDocument(document);
        }
    }

我在MongoServiceTest中测试此课程为

public class MongoServiceTest {
    @Autowired
    private MongoService mongoService;


    @Test
    public void testMongoService() {
        final DBObject document = DBContract.getUniqueQuery("001");
        document.put(DBContract.R_VARIABLES, "values");
        document.put(DBContract.P_VARIABLES, "values");

        mongoService.putDocument(document);
    }   

当我跑步时,我遇到了NullpointerException

10:24:23.956 [main] INFO  c.s.sparrow.business.util.MongoRule -  Setting up Mongo Database
10:24:23.963 [main] INFO  c.s.sparrow.business.util.MongoRule - Shutting down the Mongo Database

java.lang.NullPointerException
    at com.business.persist.MongoServiceTest.testMongoService(MongoServiceTest.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    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.rules.ExternalResource$1.evaluate(ExternalResource.java:46)
    at org.junit.rules.RunRules.evaluate(RunRules.java:18)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    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.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:76)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

似乎连线实际上并没有创建一个bean并调用init方法

问题

我该如何解决这个问题并让它发挥作用?

谢谢

2 个答案:

答案 0 :(得分:2)

使用@ContextConfiguration

@ContextConfiguration
public class MongoServiceTest {
    @Resource
    private MongoService mongoService;

完整的测试类应该看起来像

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public final class MongoServiceTest {

   @Resource
   private MongoServiceservice;

   @Test
   public void testServiceName() {//...
} 

答案 1 :(得分:0)

如@Jigar所述,这是我添加的内容,它开始起作用了!

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/com/business/wireup-spring.xml")
public class MongoServiceTest {

    @Rule
    public final MongoRule mongoRule;
    @Resource
    private MongoService mongoService;

    public MongoServiceTest() throws UnknownHostException {
        mongoRule = new MongoRule();
    }

    @Test
    public void testMongoService() {
        final DBObject document = DBContract.getUniqueQuery("001");
        document.put(DBContract.R_VARIABLES, "values");
        document.put(DBContract.P_VARIABLES, "values");

        mongoService.putDocument(document);
    }