1

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メソッドを呼び出しているようには見えません

質問

どうすればこれを修正して機能させることができますか?

ありがとうございました

4

2 に答える 2

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() {//...
} 
于 2012-07-02T17:28:05.880 に答える
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);
    }
于 2012-07-02T18:31:13.280 に答える