[英]setUp() not being called in JUnit
For some reason, the setUp() method of my test class is not being called before my test method.出于某种原因,我的测试类的 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");
}
}
Specifically, I am getting a null pointer exception on the line that says具体来说,我在行上收到一个空指针异常
assertTrue(auction.recordNewBid(bid), "first bid should be added without error");
because auction has not been initialized.因为拍卖还没有初始化。 I am using Eclipse.
我正在使用 Eclipse。
You're using a JUnit 5 @Test
but JUnit 4 @Before
/ @After
.您使用的是 JUnit 5
@Test
但 JUnit 4 @Before
/ @After
。
You need to use @BeforeEach
/ @AfterEach
from org.junit.jupiter
.您需要使用来自
org.junit.jupiter
@BeforeEach
/ @AfterEach
。
From what it looks like, you can try changing the import from从它的外观来看,您可以尝试更改导入
import org.junit.jupiter.api.Test;
to到
import org.junit.Test;
Well, For me this is how it worked.好吧,对我来说,这就是它的工作方式。 I have used:
我用过了:
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.BeforeEach;
And it worked.它奏效了。 I have also used
我也用过
import org.junit.Before; import org.junit.Test;
It also worked.它也起作用了。 Any other combination doesn't worked for me.
任何其他组合对我都不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.