简体   繁体   English

JUnit和SPring引导-对象在控制器中定义良好,但在测试中未定义

[英]JUnit and SPring Boot - object well defined in controller but not in test

I'm new with all of that, so the answer should be obvious but I can't get it by myself :op 我是所有这些的新手,所以答案应该很明显,但我自己却无法解决:op

I'm working on a simple Spring Boot application and I'm "trying" to set up some JUnit test. 我正在开发一个简单的Spring Boot应用程序,并且正在“尝试”设置一些JUnit测试。

In my controller I've this code : 在我的控制器中,我有以下代码:

@Controller
public class UserController {

    @Autowired
    private OrderInfoService orderInfoService;

    @RequestMapping(value = "/single", method = RequestMethod.GET)
    public ResponseEntity<List<OrderInfo>> orderinfo() {

        List<OrderInfo> orderInfo = orderInfoService.getOrderInfo("ca1121a");
        System.out.println("Created output string :" + orderInfo.toString());
        return new ResponseEntity<List<OrderInfo>>(orderInfo, HttpStatus.OK);
    }
}

This is only displaying a test page at "/single". 这仅在“ / single”处显示测试页。 The content of "orderInfo" appear both in the command line and on my web page. “ orderInfo”的内容同时出现在命令行和我的网页上。 Good! 好!

Now I'm trying to setup a JUnit test like this : 现在,我正在尝试像这样设置JUnit测试:

public class OrderInfoServiceImplTest {
    // Call class under test
    private OrderInfoService orderInfoService;

    @Test
    public void testGetOrderInfo() {
        System.out.println("Test - getOrderInfo");

        String res = "[OrderInfo :typeNameca1121a - retroFit ]" ;   
        List<OrderInfo> orderInfo = orderInfoService.getOrderInfo("ca1121a");
        System.out.println("Created output orderInfo");

        System.out.println("\t" + orderInfo.size());

        Assert.assertEquals(res, orderInfo.get(0).toString());
    }
}

This give me a null pointer exception : 这给了我一个空指针异常:

[INFO] Running OrderInfoServiceImplTest Test - getOrderInfo [ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.178 s <<< FAILURE! [INFO]运行OrderInfoServiceImplTest测试-getOrderInfo [错误]测试运行:1,失败:0,错误:1,跳过:0,经过时间:0.178 s <<<失败! - in OrderInfoServiceImplTest [ERROR] testGetOrderInfo(OrderInfoServiceImplTest) Time elapsed: 0.115 s <<< ERROR! -在OrderInfoServiceImplTest中[错误] testGetOrderInfo(OrderInfoServiceImplTest)经过的时间:0.115 s <<<错误! java.lang.NullPointerException at com.devglan.service.impl.OrderInfoServiceImplTest.testGetOrderInfo(OrderInfoServiceImplTest.java:23) com.devglan.service.impl.OrderInfoServiceImplTest.testGetOrderInfo(OrderInfoServiceImplTest.java:23)上的java.lang.NullPointerException

I don't understand why my object is well defined in the controller but not in the test, I use exactly the same command. 我不明白为什么我的对象在控制器中定义良好,但在测试中却没有定义,我使用完全相同的命令。 Does it related to the @autowired in the controller (which I have to say I don't understand yet)? 它与控制器中的@autowired有关吗(我不得不说我还不明白)?

Any help would be appreciate. 任何帮助将不胜感激。 Thank you 谢谢

Some annotations, which make your test case class be run under the control of spring test framework and your object under test is auto-wired to your test case class,are missed. 缺少一些注释,这些注释使您的测试用例类在spring测试框架的控制下运行,并且被测对象自动连接到您的测试用例类。 As for your example , the following code will work. 对于您的示例,以下代码将起作用。

@RunWith(SpringRunner.class)
@SpringBootTest
public class OrderInfoServiceImplTest {
    // autowire class under test
    @Autowired
    private OrderInfoService orderInfoService;

    @Test
    public void testGetOrderInfo() {
        System.out.println("Test - getOrderInfo");

        String res = "[OrderInfo :typeNameca1121a - retroFit ]" ;   
        List<OrderInfo> orderInfo = orderInfoService.getOrderInfo("ca1121a");
        System.out.println("Created output orderInfo");

        System.out.println("\t" + orderInfo.size());

        Assert.assertEquals(res, orderInfo.get(0).toString());
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM