JUnit4的BeforeClass标签未调用方法

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

JUnit4 BeforeClass tag not calling method

问题

我目前在设置JUnit测试时遇到了问题。在我的测试类中,我有一个方法用于初始化测试将使用的对象,但由于某种原因该方法没有被调用。

我正在使用junit 4.13.1和jdk1.8。

我的POM文件依赖项如下:

<dependencies>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.10.6</version>
    </dependency>
    <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

我的测试类如下:

import org.joda.time.DateTime;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.BeforeClass;
import org.junit.Test;

public class StudentTest {
    private Student s1, s2;
    private Module m1, m2;
    private Course c1, c2;
    
    @BeforeClass
    public void setup(){
        s1 = new Student("Mohamed Moustafa", 32, new DateTime("1988-11-21"), 1728);
        s2 = new Student("Mohamed Moustafa", 34, new DateTime("1986-11-01"), 1708);
        
        m1 = new Module("Programming", "CT101");
        m2 = new Module("Paradigms", "CT201");
        
        c1 = new Course("CS&IT", new DateTime("2020-08-01"), new DateTime("2021-05-25"));
        c2 = new Course("ECE", new DateTime("2020-08-01"), new DateTime("2021-05-25"));
    }
    
    @Test
    public void testSetup(){
        assertNotNull(s1);
    }
}

然而,在Maven Surefire报告中我得到了以下内容:

> -------------------------------------------------------------------------------
> 
> Test set: StudentTest
> 
> -------------------------------------------------------------------------------
> 
> Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.002
> 
> sec <<< FAILURE! StudentTest.testSetup()  Time elapsed: 0.001 sec  <<<
> 
> FAILURE! java.lang.AssertionError 	at
> 
> org.junit.Assert.fail(Assert.java:87) 	at
> 
> org.junit.Assert.assertTrue(Assert.java:42) 	at
> 
> org.junit.Assert.assertNotNull(Assert.java:713) 	at
> 
> org.junit.Assert.assertNotNull(Assert.java:723) 	at
> 
> StudentTest.testSetup(StudentTest.java:32)

这意味着对象没有被初始化,并且setup方法没有被调用。我对JUnit还不熟悉,但我在网上看到的示例要么使用Before标签,要么使用BeforeClass标签来处理类似的方法,我是不是做错了什么?

英文:

I am currently having an issue setting up JUnit tests. I have a method in my test class to initialize objects that the tests will use, but for some reason the method is not being called.

I am using junit 4.13.1 with jdk1.8

My POM file dependencies:

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;joda-time&lt;/groupId&gt;
            &lt;artifactId&gt;joda-time&lt;/artifactId&gt;
            &lt;version&gt;2.10.6&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.junit&lt;/groupId&gt;
            &lt;artifactId&gt;junit&lt;/artifactId&gt;
            &lt;version&gt;4.13.1&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;

    &lt;/dependencies&gt;

My Test class:

import org.joda.time.DateTime;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 *
 * @author mohdm
 */
public class StudentTest {
    private Student s1, s2;
    private Module m1, m2;
    private Course c1, c2;
    
    @BeforeClass
    public void setup(){
        s1 = new Student(&quot;Mohamed Moustafa&quot;, 32, new DateTime(&quot;1988-11-21&quot;), 1728);
        s2 = new Student(&quot;Mohamed Moustafa&quot;, 34, new DateTime(&quot;1986-11-01&quot;), 1708);
        
        m1 = new Module(&quot;Programming&quot;, &quot;CT101&quot;);
        m2 = new Module(&quot;Paradigms&quot;, &quot;CT201&quot;);
        
        c1 = new Course(&quot;CS&amp;IT&quot;, new DateTime(&quot;2020-08-01&quot;), new DateTime(&quot;2021-05-25&quot;));
        c2 = new Course(&quot;ECE&quot;, new DateTime(&quot;2020-08-01&quot;), new DateTime(&quot;2021-05-25&quot;));
    }
    
    @Test
    public void testSetup(){
        assertNotNull(s1);
    }
}

Yet I am getting this in maven surefire report:

> -------------------------------------------------------------------------------
>
> Test set: StudentTest
>
> -------------------------------------------------------------------------------
>
> Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.002
>
> sec <<< FAILURE! StudentTest.testSetup() Time elapsed: 0.001 sec <<<
>
> FAILURE! java.lang.AssertionError at
>
> org.junit.Assert.fail(Assert.java:87) at
>
> org.junit.Assert.assertTrue(Assert.java:42) at
>
> org.junit.Assert.assertNotNull(Assert.java:713) at
>
> org.junit.Assert.assertNotNull(Assert.java:723) at
>
> StudentTest.testSetup(StudentTest.java:32)

Meaning that the objects arent being initialized and that the setup method isnt being called. I am new to JUnit, but the examples I saw online either use Before or BeforeClass tags for similar methods, am I doing something wrong?

答案1

得分: 1

以下是翻译好的内容:

您需要将这些方法设置为静态方法,这样它们将在运行类的测试之前执行。

https://github.com/junit-team/junit4/issues/122

@BeforeClass
public static void setup(){
    s1 = new Student("Mohamed Moustafa", 32, new DateTime("1988-11-21"), 1728);
    s2 = new Student("Mohamed Moustafa", 34, new DateTime("1986-11-01"), 1708);
    
    m1 = new Module("Programming", "CT101");
    m2 = new Module("Paradigms", "CT201");
    
    c1 = new Course("CS&IT", new DateTime("2020-08-01"), new DateTime("2021-05-25"));
    c2 = new Course("ECE", new DateTime("2020-08-01"), new DateTime("2021-05-25"));
}
英文:

You need these methods to be static, so they'll be executed before running the tests of the class.

https://github.com/junit-team/junit4/issues/122

 @BeforeClass
 public static  void setup(){
    s1 = new Student(&quot;Mohamed Moustafa&quot;, 32, new DateTime(&quot;1988-11-21&quot;), 1728);
    s2 = new Student(&quot;Mohamed Moustafa&quot;, 34, new DateTime(&quot;1986-11-01&quot;), 1708);
    
    m1 = new Module(&quot;Programming&quot;, &quot;CT101&quot;);
    m2 = new Module(&quot;Paradigms&quot;, &quot;CT201&quot;);
    
    c1 = new Course(&quot;CS&amp;IT&quot;, new DateTime(&quot;2020-08-01&quot;), new DateTime(&quot;2021-05-25&quot;));
    c2 = new Course(&quot;ECE&quot;, new DateTime(&quot;2020-08-01&quot;), new DateTime(&quot;2021-05-25&quot;));
}

huangapple
  • 本文由 发表于 2020年10月16日 08:07:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64381231.html
匿名

发表评论

匿名网友

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

确定