Java JUnit中未调用setUp()

Java JUnit中未调用setUp(),java,unit-testing,testing,junit,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 pu

由于某些原因,在调用测试方法之前,没有调用测试类的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(出价),“应添加第一个出价,不得有错误”)


因为拍卖尚未初始化。我正在使用Eclipse

您使用的是JUnit 5
@Test
,而JUnit 4
@在
/
@之前
之后


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

从外观上看,您可以尝试从更改导入

import org.junit.jupiter.api.Test;


对我来说,这就是它的工作原理。 我用过:

import org.junit.jupiter.api.Test;导入org.junit.jupiter.api.beforeach


成功了。我还使用了

import org.junit.Before;
导入org.junit.Test


它也起了作用。任何其他组合对我都不起作用

import org.junit.Test;