英文:
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("add");
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("add");
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:
<?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>
答案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.
专注分享java语言的经验与见解,让所有开发者获益!
评论