1

I'm following tutorials for integrating Spring docs into my project but i'm running into nullpointerexception when I run my test.

The errors go away when I take out all the document bits. So when I remove restDocumentation variable, the document bit from the setup method and the test then it passes.

Here is my test class

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigWebContextLoader.class, classes = { ContextConfiguration.class })
@WebAppConfiguration
public class ApiDocs {

@Rule
public RestDocumentation restDocumentation = new RestDocumentation(
        "${project.basedir}/target/generated-snippets");

private RestDocumentationResultHandler document;

@Autowired
private WebApplicationContext context;

private MockMvc mockMvc;

@Autowired
Config Config;

@Before
public void setUp() {

    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
            .apply(documentationConfiguration(this.restDocumentation)).alwaysDo(this.document).build();

}

@Test
public void getConfig() throws Exception {

    this.mockMvc.perform(get("/config").accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk()).andDo(document("index"));
 }
 }

The error that I'm getting(I've slashed out my class package due to privacy):

 java.lang.NullPointerException
 at org.springframework.test.web.servlet.MockMvc.applyDefualtResultActions(MockMvc.java:195)
 at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:163)
 at //.//.//.//.//.ApiDocs.getConfig(ApiDocs.java:67)
 at org.springframework.test.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
 at org.springframework.test.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
 at
org.springframework.restdocs.RestDocumentation$1.evaluate(RestDocumentation.java:59)
 at org.springframework.test.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
 at org.springframework.test.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
 at org.springframework.test.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
  at org.springframework.test.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:75)
  at org.springframework.test.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:75)
   at org.springframework.test.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
Qad
  • 51
  • 3
  • 7
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – xenteros Aug 26 '16 at 10:46

1 Answers1

3

Nothing's assigning a value to your this.document field so it's null. You're passing that into alwaysDo which then causes a NullPointerException.

You need to configure what you want to always happen. For example by adding this to the beginning of your setUp method:

this.document = document("{method-name}", 
        preprocessRequest(removeHeaders("Foo")),
        preprocessResponse(prettyPrint()));

There's more information about this in the documentation.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • 1
    Than please accept it as a correct answer and maybe upvote it:) I made the same stupid mistake, I guess the tutorial I followed missed this and I did not notice. Anyway. One helpful thing to do is to use `"{class-name}/{method-name}"` instead of `"{method-name}" to better differentiate between tests. – Ondrej Burkert Sep 14 '16 at 12:35