【问题标题】:setUp() not being called in JUnitJUnit 中未调用 setUp()
【发布时间】:2019-03-20 05:10:12
【问题描述】:

由于某种原因,我的测试类的 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。

【问题讨论】:

    标签: java unit-testing testing junit


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      从它的样子,你可以尝试更改导入 from

      import org.junit.jupiter.api.Test;
      

      import org.junit.Test;
      

      【讨论】:

        【解决方案3】:

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

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


        它奏效了。我也用过

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


        它也有效。任何其他组合都不适合我。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-24
          • 2015-09-03
          • 1970-01-01
          相关资源
          最近更新 更多