繁体   English   中英

未在 JUnit 中调用 setUp()

[英]setUp() not being called in 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), "first bid should be added without error");

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

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

您需要使用来自org.junit.jupiter @BeforeEach / @AfterEach

从它的外观来看,您可以尝试更改导入

import org.junit.jupiter.api.Test;

import org.junit.Test;

好吧,对我来说,这就是它的工作方式。 我用过了:

import org.junit.jupiter.api.Test; import org.junit.jupiter.api.BeforeEach;


它奏效了。 我也用过

import org.junit.Before; import org.junit.Test;


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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM