JUnit在setUpClass或setUp中声明时出现空指针异常。

huangapple 未分类评论47阅读模式
英文:

JUnit Null Pointer Exception When Declaring in setUpClass or setUp

问题

以下是您提供的代码的翻译部分:

第一种方式:

public class CalculatorTest {

    Calculator c;

    @BeforeEach
    public void setUp() {
        c = new Calculator();
    }

    @Test
    public void testAdd() {
        System.out.println("add");
        int a = 0;
        int b = 0;
        int expResult = 0;
        int result = c.add(a, b);
        assertEquals(expResult, result);
    }
    // ...

第二种方式:

public class CalculatorTest {

    static Calculator c;

    public CalculatorTest() {
    }

    @BeforeAll
    public static void setUpClass() {
        c = new Calculator();
    }

    @Test
    public void testAdd() {
        System.out.println("add");
        int a = 0;
        int b = 0;
        int expResult = 0;
        int result = c.add(a, b);
        assertEquals(expResult, result);
    }
    // ...

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>mavenproject1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-engine</artifactId>
            <version>1.6.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>
    </properties>
</project>

请注意,我只翻译了您提供的代码部分,不包括问题和其他内容。如果您还有其他问题或需要进一步的帮助,请随时提问。

英文:

Currently I am setting up basic JUnit tests in the following two manners which both give me null pointer exceptions when running the tests at the line where I call c.add():

public class CalculatorTest {

    Calculator c;

    @BeforeEach
    public void setUp() {
        c = new Calculator();
    }
    
    @Test
    public void testAdd() {
        System.out.println(&quot;add&quot;);
        int a = 0;
        int b = 0;
        int expResult = 0;
        int result = c.add(a, b);
        assertEquals(expResult, result);
    }
....

The second way:

public class CalculatorTest {
    
    static Calculator c;
    
    public CalculatorTest() {
    }
    
    @BeforeAll
    public static void setUpClass() {
        c = new Calculator();
    }

    @Test
    public void testAdd() {
        System.out.println(&quot;add&quot;);
        int a = 0;
        int b = 0;
        int expResult = 0;
        int result = c.add(a, b);
        assertEquals(expResult, result);
    }
...

This link: https://stackoverflow.com/questions/19978692/junit-null-pointer-exception
suggests to me I'm doing this right but I'm still getting the null pointer exception.

My pom.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;com.mycompany&lt;/groupId&gt;
    &lt;artifactId&gt;mavenproject1&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
            &lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;
            &lt;version&gt;5.6.0&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
            &lt;artifactId&gt;junit-jupiter-params&lt;/artifactId&gt;
            &lt;version&gt;5.6.0&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
            &lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
            &lt;version&gt;5.6.0&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.junit.platform&lt;/groupId&gt;
            &lt;artifactId&gt;junit-platform-engine&lt;/artifactId&gt;
            &lt;version&gt;1.6.0&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
    &lt;properties&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
        &lt;maven.compiler.source&gt;14&lt;/maven.compiler.source&gt;
        &lt;maven.compiler.target&gt;14&lt;/maven.compiler.target&gt;
    &lt;/properties&gt;
&lt;/project&gt;

答案1

得分: 0

计算器是被测试的类,因此您不需要执行以下操作:

@BeforeEach
public void setUp() {
    c = new Calculator();
}

您可以像这样声明 c:

Calculator c = new Calculator();

并像往常一样添加 @Test 方法。

英文:

Calculator is Class Under Test, therefore, you don't need to do

@BeforeEach
public void setUp() {
    c = new Calculator();
}

You can declare c like this:

Calculator c = new Calculator();

and add @Test method as usual.

huangapple
  • 本文由 发表于 2020年7月26日 04:41:25
  • 转载请务必保留本文链接:https://java.coder-hub.com/63093374.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定