函数在主类中工作,但不在测试类中

时间:2013-05-09 14:41:33

标签: java junit neo4j

我有一个按预期编译和运行的类(每次执行添加一个测试节点):

public class ReqsDb {
    private final String STORE_DIR;
    public GraphDatabaseService graphDb;

    private static enum RelTypes implements RelationshipType {
        IDENTIFIES, SATIFIES
    }

    public ReqsDb(String dbPath) {
        STORE_DIR = dbPath;
        graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(STORE_DIR);
        registerShutdownHook(graphDb);
    }

    public void createTestNode() {
        Transaction tx = graphDb.beginTx();
        Node newNode;
        try {
            newNode = graphDb.createNode();
            newNode.setProperty("test", "test");
            tx.success();
        } finally {
            tx.finish();
        }
    }

    private static void registerShutdownHook(final GraphDatabaseService graphDb) {
        Runtime.getRuntime().addShutdownHook(new Thread() {
                    @Override
                    public void run() {
                        graphDb.shutdown();
                    }
        });
    }

    void shutDown() {
        graphDb.shutdown();
    }

    public static void main(String[] args) {
        ReqsDb testDb = new ReqsDb("target/testDb");
        testDb.createTestNode();
    }
}

但是测试函数testCreateTestNode()会导致错误:

java.lang.RuntimeException: org.neo4j.kernel.lifecycle.LifecycleException: Component 'org.neo4j.kernel.StoreLockerLifecycleAdapter@4e3a2be1' was successfully initialized, but failed to start.

由于函数是从main()调用的,我认为测试类有问题。

package com.github.dprentiss;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class ReqsDbTest extends TestCase {
    protected ReqsDb testDb = new ReqsDb("target/testDb");

    public ReqsDbTest(String testName) {
        super(testName);
    }

    public static Test suite() {
        return new TestSuite(ReqsDbTest.class);
    }

    public void testDbService() {
        assertNotNull(testDb);
    }

    public void testCreateTestNode() {
        testDb.createTestNode();
    }

    public void tearDown() {
        testDb.shutDown();
    }

我的测试设置有问题吗?

1 个答案:

答案 0 :(得分:0)

尝试放

protected ReqsDb testDb = new ReqsDb("target/testDb");

在init方法中。请遵循以下示例:

Is there a basic template I can use to create a test?