英文:
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:
    <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>
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("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);
    }
}
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("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"));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论