简体   繁体   English

Java Spring Test Autowired Controller is null 错误

[英]Java Spring Test Autowired Controller is null error

I would like to test a Java Web Controller using Spring (Not Spring Boot).我想使用 Spring(不是 Spring Boot)测试 Java Web 控制器。

My Controller is我的控制器是

@Controller
@RequestMapping("/orders")
public class OrderHdrController {
    @RequestMapping(value = "/getOrderList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getOrderTables(OrderSearchDto orderSearchDto) { ... }
}

And my test class is:我的测试课是:

public class FilterActivityTest2 {

    @Autowired
    private OrderHdrController orderHdrController;

    @Test
    public void testActivity() {
        OrderSearchDto orderSearchDto = new OrderSearchDto();
        OrderSearchPanelDto orderSearchPanelDto = new OrderSearchPanelDto();
        orderSearchPanelDto.setActivityTypes(Arrays.asList("TAKEOVER","DELIVERY"));
        orderSearchDto.setOrderSearchPanelDto(orderSearchPanelDto);
        Map<String, Object>  result =  orderHdrController.getOrderTables(orderSearchDto);
        assertNotNull(result);
    }
}

I do not want to mock any objects.我不想嘲笑任何对象。 I just want to run the test on the controller all the way to the db.我只想在控制器上一直运行测试到数据库。 But when I debug into the test, the orderHdrController is null in testActivity method.但是当我调试到测试中时, testActivity 方法中的 orderHdrController 为空。

What have I done wrong?我做错了什么? Please help or ask me for more information.请帮助或向我询问更多信息。 Thanks.谢谢。

@RunWith(SpringRunner.class)
@WebMvcTest(OrderHdrController.class)
public class FilterActivityTest2 {

    @Autowired
    private OrderHdrController orderHdrController;

    @Test
    public void testActivity() {
        OrderSearchDto orderSearchDto = new OrderSearchDto();
        OrderSearchPanelDto orderSearchPanelDto = new OrderSearchPanelDto();
        orderSearchPanelDto.setActivityTypes(Arrays.asList("TAKEOVER","DELIVERY"));
        orderSearchDto.setOrderSearchPanelDto(orderSearchPanelDto);
        Map<String, Object>  result =  orderHdrController.getOrderTables(orderSearchDto);
        assertNotNull(result);
    }
}

or If dont using any spring or junit then why using @test或者如果不使用任何 spring 或 junit 那么为什么使用 @test

simply make a main class只需创建一个主类

public class FilterActivityTest2{

    public static void main(String args[]){

 ....... put your tast case code here 

    }
}

Your FilterActivityTest2 needs to be managed by the Spring context, to be able to autowire dependencies.您的FilterActivityTest2需要由 Spring 上下文管理,以便能够自动装配依赖项。

To do that, either annotate your test class with:为此,请使用以下命令注释您的测试类:

@RunWith(SpringRunner.class)
@SpringBootTest

Or extend the main test class that already has these annotations.或者扩展已经有这些注解的主测试类。 If you created your project using spring initializer, you'll find that class in the tests created for you.如果您使用 spring 初始化程序创建了您的项目,您将在为您创建的测试中找到该类。

public class FilterActivityTest2 extends MyApplicationTests {

EDIT编辑

For Spring, you can use @ContextConfiguration .对于 Spring,您可以使用@ContextConfiguration Here is a good tutorial. 是一个很好的教程。
Also see the official documentation here .另请参阅此处的官方文档。

If you do not use spring boot then you can create the application context manually如果不使用 spring boot,则可以手动创建应用程序上下文

    @Before
    public void init() {
        ApplicationContext context = desired implementation;
        controller = context.getBean("bean name");
    }

But better do this但最好这样做

@RunWith(MockitoJUnitRunner.class)
public class FilterActivityTest2 {

    private OrderHdrController orderHdrController;

    @MockBean
    private Service service;
    @MockBean
    private  Dao dao;

    @Before
    public void init() {
        orderHdrController = new OrderHdrController(service, dao ....);
    }

    @Test
    ....
}

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

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