简体   繁体   English

为什么我的单元测试一直显示 NullPointerException? 我正在使用@BeforeClass,但它似乎不起作用

[英]Why does my Unit Test keep saying NullPointerException? I'm using @BeforeClass and it doesn't seem to work

public class FlooringMasteryServiceLayerImplTest {

    private FlooringMasteryServiceLayer service;

    /*public FlooringMasteryServiceLayerImplTest() {
        ApplicationContext ctx
                = new ClassPathXmlApplicationContext("applicationContext.xml");
        service
                = ctx.getBean("serviceLayer", FlooringMasteryServiceLayer.class);
    }*/

    @BeforeClass
    public static void setUpClass() {
    }

    @AfterClass
    public static void tearDownClass() {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    
    @Test
    public void testCreatedOrderFileNameFromDate() {
        
        LocalDate date = LocalDate.parse("2020-11-25");
        
        String orderFileName = service.createOrderFileNameFromDate(date);
        
        assertEquals(orderFileName, "Orders_11252020.txt", "The order file name generated was incorrect.");
    }

}

I can't seem to find what the solution is for this Failed Unit Test.我似乎找不到这个失败的单元测试的解决方案。 I have @BeforeClass so I believe it is running JUnit4.我有@BeforeClass,所以我相信它正在运行 JUnit4。 It just keeps saying NullPointerException and I don't know how to solve this issue.它只是一直说 NullPointerException 我不知道如何解决这个问题。 Please help请帮忙

are you instantiating the service object?您是否正在实例化服务 object?

You need to initialize your variable "service" inside @BeforeClass method.您需要在 @BeforeClass 方法中初始化变量“服务”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么执行子类测试方法时我的@BeforeClass方法不能运行? - Why doesn't my @BeforeClass method run when executing a subclass test method? 我正在使用Servlet,但似乎无法弄清楚为什么我的下载文件Servlet可以在本地运行,但不能正常运行 - I'm using Servlets and can't seem to figure out why my download file servlet will work locally, but not otherwise 我的测试没有在控制台中输出我正在使用 Maven - My test doesn't output in console I'm using Maven 为什么它一直在我的代码上说“ArrayIndexOutOfBoundsException”? - Why does it keep saying “ArrayIndexOutOfBoundsException” on my code? 为什么此单元测试说我的目录不存在? - Why does this unit test say my directory doesn't exist when it does? 我的单元测试中为NullPointerException - NullPointerException in my unit test 将JUnit @BeforeClass与Cucumber一起使用…是否有效? - Using JUnit @BeforeClass with Cucumber…does it work? 为什么我的WebView不起作用? - Why does my WebView doesn't work? 为什么我的GridView不起作用? - Why does my GridView doesn't work? 为什么我的编译器总是说我有一个catch块而没有先前的try块 - Why does my compiler keep saying I have a catch block without a previous try block
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM