java - JUnit 中未调用 setUp()

标签 java unit-testing testing junit

由于某种原因,我的测试类的 setUp() 方法没有在我的测试方法之前被调用。

import static org.junit.jupiter.api.Assertions.*;
import org.junit.After;
import org.junit.Before;
import org.junit.jupiter.api.Test;

class BlockchainAuctionTest {
    private BlockchainAuction auction;

@Before
public void setUp() {
    auction = new BlockchainAuction();
    System.out.println("setUp");
}

@After
public void tearDown() {
    System.out.println("tearDown");
}

@Test
void testOneBid() {
    Bid bid = new Bid("Bitcoin", "Devon", 1.0);
    assertTrue(auction.recordNewBid(bid), "first bid should be added without error");
}
}

具体来说,我在

行中遇到空指针异常

assertTrue(auction.recordNewBid(bid), "首次出价应无误添加");

因为拍卖还没有初始化。我正在使用 Eclipse。

最佳答案

您使用的是 JUnit 5 @Test,但使用的是 JUnit 4 @Before/@After

您需要使用 org.junit.jupiter 中的 @BeforeEach/@AfterEach

关于java - JUnit 中未调用 setUp(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55253913/

相关文章:

使用 Windows 窗体进行 C# 单元测试

android - DaggerMock 正在生成意外实例

android - 运行经过检测的 Android 测试会产生 "Unknown command-line option ' --tests' "

java - JUnit 找不到 ApplicationContext

javascript - 开 Jest ReferenceError : Cannot access '' before initialization

java - 使用 `verify` 显式检查函数调用参数或使用 `when` 隐式检查函数调用参数?

java - 如何在ant build xml中设置java_home

java - ReSTLet - 如何从 GET 请求获取 cookie

java - 如何返回 DataSnapshot 值作为方法的结果?

java - 使用 spring 注解注入(inject) p 命名空间属性