4

This question is next to the question I had asked here. I am using spring-test-mvc framework to test REST controller. Please see the above link to see my code for the REST Controller I have.

My test for GET is working fine but I can't do a POST. I get an exception when I try do so. This is my first time testing a REST controller.

Here's my PcUserControllerTest class

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/applicationContextTest.xml")
public class PcUserControllerTest {

    @Autowired
    PcUserService pcUserService;

    @Autowired
    PcUserController pcUserController;

    PcUser pcUser;

    private MockMvc mockMvc;

    @Before
    public void setUp() throws Exception {
        mockMvc = standaloneSetup(pcUserController).build();
        pcUser = new PcUser("John", "Li", "Weasley", "john", "john");
        pcUserService.create(pcUser);
    }

    @After
    public void tearDown() throws Exception {
        pcUserService.delete(pcUser.getId());
    }

    @Test
    public void shouldGetPcUser() throws Exception {
        this.mockMvc.perform(get("/pcusers/" + pcUser.getId()).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
    }

    @Test
    public void shouldCreatePcUser() throws Exception {
        PcUser newUser = new PcUser("Mike", "Li", "Donald", "mike", "mike");
        this.mockMvc.perform(post("/pcusers/create/" + newUser).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
    }
}

Exception I get when I execute shouldCreatePcUser:

java.lang.IllegalArgumentException: Not enough variable values available to expand ["firstName"]
    at org.springframework.web.util.UriComponents$VarArgsTemplateVariables.getValue(UriComponents.java:1011)
    at org.springframework.web.util.UriComponents.expandUriComponent(UriComponents.java:443)
    at org.springframework.web.util.UriComponents.access$1(UriComponents.java:431)
    at org.springframework.web.util.UriComponents$FullPathComponent.expand(UriComponents.java:790)
    at org.springframework.web.util.UriComponents.expandInternal(UriComponents.java:413)
    at org.springframework.web.util.UriComponents.expand(UriComponents.java:404)
    at org.springframework.web.util.UriTemplate.expand(UriTemplate.java:121)
    at org.springframework.test.web.server.request.MockMvcRequestBuilders.expandUrl(MockMvcRequestBuilders.java:51)
    at org.springframework.test.web.server.request.MockMvcRequestBuilders.request(MockMvcRequestBuilders.java:45)
    at org.springframework.test.web.server.request.MockMvcRequestBuilders.post(MockMvcRequestBuilders.java:28)
    at com.project.cleaner.controller.test.PcUserControllerTest.shouldCreatePcUser(PcUserControllerTest.java:69)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

My PcUser looks like

@Document
public class PcUser extends BaseEntity {
    private String firstName;
    private String middleName;
    private String lastName;
    private String username;
    private String password;

    public PcUser() {
        super();
    }

    public PcUser(String firstName, String middleName, String lastName,
            String username, String password) {
        super();
        this.firstName = firstName;
        this.middleName = middleName;
        this.lastName = lastName;
        this.username = username;
        this.password = password;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getMiddleName() {
        return middleName;
    }

    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
//      JSONObject jsonPcUser = new JSONObject();
//      jsonPcUser.put("id", this.getId());
//      jsonPcUser.put("firstName", this.firstName);
//      jsonPcUser.put("middleName", this.middleName);
//      jsonPcUser.put("lastName", this.lastName);
//      jsonPcUser.put("username", this.username);
//
//      return jsonPcUser.toJSONString();

        return "{\"firstName\":" + this.firstName + ", "
                + "\"middleName\":" + this.middleName + ", "
                + "\"lastName\":" + this.lastName + ", "
                + "\"username\":" + this.username
                + "}";
    }
}

BaseEntity:

public class BaseEntity {
    @Id
    private String id;

    public BaseEntity() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

I don't understand this exception. If anyone could guide me in the right direction would be really appreciated.

Community
  • 1
  • 1
jsf
  • 2,511
  • 8
  • 28
  • 33

2 Answers2

3

It seems, as beerbajay mentions, that the problem is that what you are trying to post to your controller is a PcUser as a URL/path parameter instead of in the actual body of the HTTP request.

I am using the org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder#content(String) method to set the body of the HTTP request itself (as opposed to substitute parts of the URL or add URL parameters) like this:

PcUser newUser = new PcUser("Mike", "Li", "Donald", "mike", "mike");
this.mockMvc.perform(post("/pcusers/create/")
            .content(newUser.toString()) // <-- sets the request content !
            .accept(MediaType.APPLICATION_JSON)
            .andExpect(status().isOk());
Ytsejammer
  • 1,304
  • 2
  • 12
  • 24
2

Okay, you have this as your get:

get("/pcusers/" + pcUser.getId())

Which makes sense, because you want to get the resource at /pcusers/123/, but then you have:

PcUser newUser = new PcUser("Mike", "Li", "Donald", "mike", "mike");
post("/pcusers/create/" + newUser)

Which doesn't really make sense since you'll be performing a toString() on the newUser object, then concatenating that with the /pcusers/create/ string. So you're probably doing a request to something like /pcusers/create/com.mycompany.PcUser@1596138, which is probably not what you want.

beerbajay
  • 17,345
  • 6
  • 52
  • 72
  • Yes, you are right that toString() method will be called. I have overriden the toString() method and it returns data in JSON format. The idea is to do this: /pcusers/create/{firstName:Mike,middleName:Li,lastName:Rubble} and when this gets to the server it will be converted into a PcUser object. As I said this is my first time playing with a REST controller, is this incorrect. How are we supposed to pass data in a POST? – jsf Feb 06 '12 at 14:14
  • 2
    Sure, you pass the data in POST, but not like that. It needs to be in the **body** of the POST, not as part of the url. – beerbajay Feb 06 '12 at 14:33
  • Yes, that answers my question and resolves this problem. Thanks a lot for your help. Really appreciated. – jsf Feb 06 '12 at 17:34
  • @jsinghfoss how did you do then? I'm facing the same problem right now, i.e. still figuring out how to pass data to a post request using MockMvcRequestBuilders... – fbiville Mar 04 '12 at 07:53
  • alright, I found the answer here: http://stackoverflow.com/questions/7192860/testing-a-spring-controller-method-having-modelattribute-as-parameter :) – fbiville Mar 04 '12 at 08:09
  • I came across an approach this Author took. A Test class for Controller is available in the test package. The project is hosted at http://www.perfmath.com/spring/soba_zip/soba3.2.zip – jsf Jul 26 '12 at 20:04