1

New to spring boot.

API in controller looks like,

@RestController("/path1/path2")
public class SomeController
{

@GetMapping("/path3/path4")
public String doSomething()
{
//code goes here
}

}

Test case looks like,

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = 
xxx.class)
@AutoConfigureMockMvc(secure = false)
public class AuthServiceTestCase
{

@Autowired
private MockMvc mock;

@Test
public void testDoSomething()
{

//Command 1
mock.perform(get("/path1/path2/path3/path4")).andExpect(status().isOK()); 

//Command 2
 mock.perform(get("/path3/path4")).andExpect(status().isOK()); 

}

}

Now, after running test case (Command 1), I have got the following

"java.lang.AssertionError: Status expected:<200> but was:<404>"

But the "Command 2" succeeded as expected.

My question is,

RestController Prefix Path + Controller Prefix Path = Entire Path.

For invoking an API, we have to follow above format, but why Junit fails if followed same stuff?

Could some one drop some inputs here?

NANDAKUMAR THANGAVELU
  • 611
  • 3
  • 15
  • 34

2 Answers2

2

In your case /path1/path2 is a name of a controller bean. To add a general prefix path for all methods inside controller you can put

@RequestMapping("/path1/path2")

on your controller.

Vlad Hanzha
  • 464
  • 3
  • 11
2
@RestController
@RequestMapping("/path1/path2")
public class SomeController
{

@GetMapping("/path3/path4")
public String doSomething()
{
//code goes here
}

}

The problem is not your test class. Problem is the wrong usage of requestMapping.

Hatice
  • 874
  • 1
  • 8
  • 17
  • It worked. Thx. Could u pls explain, how (RestController Prefix Path + Controller Prefix Path = Entire Path) this pattern worked for invoking API and not for test case invocation. – NANDAKUMAR THANGAVELU Feb 15 '19 at 12:41
  • 1
    @NANDAKUMAR Can you check this post ? https://stackoverflow.com/questions/45210005/spring-restcontroller-annotation-does-not-catch-request – Hatice Feb 15 '19 at 12:50