JUnit测试类

时间:2017-06-26 10:20:03

标签: java spring spring-mvc junit

我为我的Bean Task编写了一个JUnit类。第一个测试testTaskShouldNotBeNull通过OK。但是第二个testTaskTitle会抛出NullPointerException。正在为我的bean的JavaConfig设置title字段,所以我不确定为什么它是null。

我的Task bean:

package com.webapps.ToDoList;
import org.springframework.stereotype.Component;

@Component
public class Task {

    private String title;

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }
}

我的JavaConfig:

package com.webapps.ToDoList;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TaskConfig {

    @Bean
    public Task task() {
        Task task = new Task();
        task.setTitle("A Generic Title!");
        return task;
    }
}

我的JUnit测试类:

package com.webapps.ToDoList;

import org.springframework.test.context.ContextConfiguration;
import junit.framework.TestCase;

@ContextConfiguration(classes = TaskConfig.class)
public class TaskTest extends TestCase {

    private Task task;

    public void testTaskShouldNotBeNull() {
        assertNull(task);
    }

    public void testTaskTitle() {
        assertNull(task.getTitle());
    }
}

更新

我尝试将@Autowired注释添加到测试类中的Task变量,这没有任何区别。

也许我的文件结构导致了问题?

enter image description here

添加@Test@RunWith(SpringRunner.class)注释后,我得到以下结果:

junit.framework.AssertionFailedError
at junit.framework.Assert.fail(Assert.java:55)
at junit.framework.Assert.assertTrue(Assert.java:22)
at junit.framework.Assert.assertNotNull(Assert.java:256)
at junit.framework.Assert.assertNotNull(Assert.java:248)
at junit.framework.TestCase.assertNotNull(TestCase.java:417)
at com.webapps.ToDoList.TaskTest.testTaskShouldNotBeNull(TaskTest.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:176)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:252)
at junit.framework.TestSuite.run(TestSuite.java:247)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:121)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

1 个答案:

答案 0 :(得分:1)

没有导入的完整测试类应如下所示(使用Spring 4.3.9.RELEASE和Junit 4.12)

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = TaskConfig.class)
public class TaskTest extends TestCase {

    @Autowired
    private Task task;

    @Test
    public void testTaskShouldNotBeNull() {
        assertNotNull(task);
    }

    @Test
    public void testTaskTitle() {
        assertNotNull(task.getTitle());
    }
}

除了缺少的@Test注释外,基本上所有的点都已在注释中完成了