1

I would like to test a Java Web Controller using Spring (Not Spring Boot).

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.

What have I done wrong? Please help or ask me for more information. Thanks.

Shazam
  • 105
  • 1
  • 7

3 Answers3

2
@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

simply make a main class

public class FilterActivityTest2{

    public static void main(String args[]){

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

    }
}
harkesh kumar
  • 833
  • 2
  • 13
  • 35
0

Your FilterActivityTest2 needs to be managed by the Spring context, to be able to autowire dependencies.

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.

public class FilterActivityTest2 extends MyApplicationTests {

EDIT

For Spring, you can use @ContextConfiguration. Here is a good tutorial.
Also see the official documentation here.

Kartik
  • 7,677
  • 4
  • 28
  • 50
  • I'm using Spring, not Spring Boot. How can I change it to Spring? – Shazam Dec 09 '19 at 06:37
  • @AlanTT Similar stuff, you can use [`@ContextConfiguration`](https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/testing.html#spring-testing-annotation-contextconfiguration). – Kartik Dec 09 '19 at 06:46
  • @AlanTT I added a link to an example in my answer. – Kartik Dec 09 '19 at 06:50
  • He is asking for away to have a spring test that is not spring boot. The other answer is correct. – Alexander Petrov Dec 09 '19 at 07:20
  • @Kartik Thanks for your answer. I followed your recommendation to create a java configuration. But in my orderHdrController, I have OrderHdrRepository which is an Interface. How can I inject an OrderHdrRepository Bean in the Config java file? I cannot instantiate **new OrderHdrRepository()** – Shazam Dec 09 '19 at 07:27
  • @AlexandarPetrov yep, OP edited the question after I had posted the answer. So I added my edit in my answer. – Kartik Dec 09 '19 at 22:47
0

If you do not use spring boot then you can create the application context manually

    @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
    ....
}
asrocket
  • 21
  • 1