java test before,Java JUnit测试不适用于@Before注释

Heay com,

started with Java JUnit testing and got a problem regarding @Before annotation.

My setup:

Java 9

Eclipse Oxygen

JUnit 5

If i do my test like this

package junittesting;

import org.junit.jupiter.api.Test;

import de.hsa.games.fatsquirrel.space.XY;

public class XYtest {

private XY testXY = new XY(0,0);

private XY addingVec = new XY(0,1);

@Test

public void addVec() {

assert (testXY.addVec(addingVec).equals(addingVec));

}

}

the test will run fine. But if i do my XY objects in the @Before annotation then it will end with an error. Nullpointer in the assert line.

package junittesting;

import org.junit.Before;

import org.junit.jupiter.api.Test;

import de.hsa.games.fatsquirrel.space.XY;

public class XYtest {

XY testXY;

XY addingVec;

@Before

public void setUp() {

testXY = new XY(0, 0);

addingVec = new XY(0, 1);

}

@Test

public void addVec() {

assert (testXY.addVec(addingVec).equals(addingVec));

}

}

Thanks in advance.

解决方案

As the JUnit 5 manual states, you must use @BeforeEach. The old @Beforeannotation only works with version 4:

@BeforeEach - Denotes that the annotated method should be executed before each @Test [...] in the current class; analogous to JUnit 4’s @Before.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值