如何在Java中使用@BeforeEach方法进行测试?

huangapple go评论119阅读模式
英文:

How can I use the @BeforeEach method for testing in Java?

问题

我正在学习使用JUnit5进行测试驱动开发。在我的阅读材料中写道:“注解添加在方法上以指定其行为。我们经常遇到的一个常见情况是,我们必须为每个测试创建一个新对象。我们可以不在每个测试方法中执行此操作,而是可以创建一个名为setUp()的新方法,然后添加@BeforeEach注解。这将确保在每个测试方法之前执行此方法。”但是我不知道如何在一个方法中使用另一个方法中的对象。请帮忙一下。这是我的代码:

  1. @BeforeEach
  2. public void setUp(){
  3. IntSet set = new IntSet(4);
  4. }
  5. @Test
  6. public void testIntSet(){
  7. assertEquals(set.getCapacity(), 4); //在这里出错:无法解析符号'set'
  8. }

注意:我只翻译了你提供的代码部分,其他内容我没有进行翻译。

英文:

I am learning Test Driven Development with JUnit5. In my reader this is written: " Annotations are added above a method to specify its behavior. A common scenario we find ourselves in is that
we have to create a new object for each test. Instead of doing this in each test method, we can create a new method called setUp() for example, and add the @beforeEach annotation. This will make sure that this method is executed before each test method." But I don't know how can I use an object from a method in another method. Any help, please? This is my code:

  1. @BeforeEach
  2. public void setUp(){
  3. IntSet set = new IntSet(4);
  4. }
  5. @Test
  6. public void testIntSet(){
  7. assertEquals(set.getCapacity(), 4); //error here: Cannot resolve symbol 'set'
  8. }

答案1

得分: 3

你所有的测试都在一个类中,对吗?将其变为一个属性/实例变量:

  1. class SomeTests {
  2. IntSet set;
  3. @BeforeEach
  4. public void setUp() {
  5. set = new IntSet(4);
  6. }
  7. @Test
  8. public void testIntSet() {
  9. assertEquals(set.getCapacity(), 4);
  10. }
  11. }
英文:

You have all your tests in a class, right? Make it a property/instance variable:

  1. class SomeTests {
  2. IntSet set;
  3. @BeforeEach
  4. public void setUp() {
  5. set = new IntSet(4);
  6. }
  7. @Test
  8. public void testIntSet() {
  9. assertEquals(set.getCapacity(), 4);
  10. }
  11. }
  12. </details>
  13. # 答案2
  14. **得分**: 2
  15. 你必须在测试类中声明 `IntSet set` 作为一个字段然后你可以在 `@BeforeEach` 方法中初始化这个字段
  16. ```java
  17. class MyTest {
  18. private IntSet set;
  19. @BeforeEach
  20. public void setUp() {
  21. set = new IntSet(4);
  22. }
  23. @Test
  24. public void testIntSet() {
  25. assertEquals(set.getCapacity(), 4); // 在这里有错误:无法解析符号 'set'
  26. }
  27. }
英文:

You have to declare IntSet set as a field in your test class. Then, you can initialize the field in the @BeforeEach method.

  1. class MyTest{
  2. private IntSet set;
  3. @BeforeEach
  4. public void setUp(){
  5. set = new IntSet(4);
  6. }
  7. @Test
  8. public void testIntSet(){
  9. assertEquals(set.getCapacity(), 4); //error here: Cannot resolve symbol &#39;set&#39;
  10. }
  11. }

答案3

得分: 1

你需要在 setUp() 方法外部声明你的变量。这样它可以在任何方法中访问。

  1. private IntSet set;
  2. @BeforeEach
  3. public void setUp(){
  4. set = new IntSet(4);
  5. }
  6. @Test
  7. public void testIntSet(){
  8. assertEquals(set.getCapacity(), 4); // 这里有错误:无法解析符号 'set'
  9. }
英文:

You need to declare your variable outside of the setUp() method. This way it is accessible for any method.

  1. private IntSet set;
  2. @BeforeEach
  3. public void setUp(){
  4. set = new IntSet(4);
  5. }
  6. @Test
  7. public void testIntSet(){
  8. assertEquals(set.getCapacity(), 4); //error here: Cannot resolve symbol &#39;set&#39;
  9. }

huangapple
  • 本文由 发表于 2020年9月10日 05:00:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63819472.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定