maven测试失败,但JUnit5正常

2ekbmq32  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(258)

我对junit5的maven测试有一个奇怪的问题。
我用junit工具为每个方法创建了测试套件,每个测试都是这样开始的。

private Class2Test createTestSubject() {
        return new Class2Test();
    }

    public void test1() throws Exception {
        Class2Test testSubject;
        String result;

        // default test
        testSubject = createTestSubject();
        result = testSubject.getData();
        //testing, assert
}

线路 result = testSubject.getData(); 返回nullpointerexception
当我通过eclipse执行相同的测试时,finishok。surefire插件已定义

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M5</version>
        <dependencies>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>5.7.0</version>
            </dependency>                
        </dependencies>
        <configuration>
                <systemPropertyVariables>
                    <entorno>${project.basedir}\resources\maqueta.properties</entorno>
                    <hibernate>${project.basedir}\resources\hibernate.cfg.xml</hibernate>
                </systemPropertyVariables>
                <parallel>classes</parallel>
                <threadCount>10</threadCount>
            </configuration>
    </plugin>

我曾试图将对象的声明更改为dogde null指针,但失败了。
class2test()是默认的构造函数,不需要参数或读取文件。

package test.com.my.myself;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import javax.annotation.Generated;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.tools.configuration.base.MethodRef;
import com.my.myself.Class2Test;
import com.google.gson.Gson;

import junitparams.JUnitParamsRunner;

@Generated(value = "org.junit-tools-1.1.0")
@RunWith(JUnitParamsRunner.class)
public class Tester1{

    private Class2Test createTestSubject() {
        return new Class2Test();
    }

    @DisplayName("TEST1")
    @MethodRef(name = "test1", signature = "()QString;")
    @Test
    public void test1() throws Exception {
        Class2Test testSubject;
        String result;

        // default test
        testSubject = createTestSubject();
        result = testSubject.test1();
    }
}

以及要测试的类

public class Class2Test{

    private Connection conn = new Connector();
    private static final Logger logger = Logger.getLogger(Class2Test.class);

    public String test1() {
         PrepareStatement pstm = conn.prepareStatement("select 1 from dual");
         ResultSet rs = pstm.executeQuery();
         ...

         return 1;
        }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题