简体   繁体   中英

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

I'm working on a simple Spring Boot application and I'm "trying" to set up some JUnit test.

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". The content of "orderInfo" appear both in the command line and on my web page. Good!

Now I'm trying to setup a JUnit test like this :

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! - in OrderInfoServiceImplTest [ERROR] testGetOrderInfo(OrderInfoServiceImplTest) Time elapsed: 0.115 s <<< ERROR! java.lang.NullPointerException at com.devglan.service.impl.OrderInfoServiceImplTest.testGetOrderInfo(OrderInfoServiceImplTest.java:23)

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)?

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. 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());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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