0

I am testing a Spring boot MVC application which takes MultipartFile as input. My service class will throw a custom exception when the file format is other than .json. I've written a JUnit test case which tests this scenario.

Instead of throwing my custom exception (expected = FileStorageException.class) my test case is throwing an AssertionError.

How to resolve this issue and validate the exception message using .andExpect(content().string("Wrong file format. Allowed: JSON."))

Exception

09:36:48.327 [main] DEBUG org.springframework.test.web.servlet.TestDispatcherServlet - Could not complete request com.test.util.exception.FileStorageException: Wrong file format. Allowed: JSON.

Code

@WebAppConfiguration
@ContextConfiguration(classes = WebConfig.class, initializers = ConfigFileApplicationContextInitializer.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class UploadTest
{

  @Autowired
  private WebApplicationContext webApplicationContext;

  private MockMvc mockMvc;

  @Before
  public void setup()
  {
    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
  }

  /**
   * 
   */
  public UploadTest()
  {
    // default constructor
  }

  @Test(expected = FileStorageException.class)
  // @Test(expected= AssertionError.class)
  public void testInvalidFileFormat() throws Exception
  {
    try
    {
      MockMultipartFile testInput = new MockMultipartFile("file", "filename.txt", "text/plain", "some json".getBytes());
      mockMvc.perform(MockMvcRequestBuilders.multipart("/uploadFile").file(testInput))
          // .andExpect(status().isInternalServerError()).andExpect(content().string("Wrong
          // file format. Allowed: JSON."))
          .andDo(print());
    }
    catch (Exception e)
    {
      fail(e.toString());
    }
  }
}
Stefan Birkner
  • 24,059
  • 12
  • 57
  • 72
Thiagarajan Ramanathan
  • 1,035
  • 5
  • 24
  • 32

1 Answers1

0

JUnit only knows about exceptions that are thrown by the test method (in your example testInvalidFileFormat(). Therefore it only can check these exceptions.

You're catching every exception that is thrown by mockMvc.perform(...) and instead throw an AssertionError from the line

fail(e.toString());

This is the AssertionError that you see in your test result.

If you want to test exceptions then you must not catch them within your test:

@Test(expected = FileStorageException.class)
public void testInvalidFileFormat() throws Exception
{
  MockMultipartFile testInput = new MockMultipartFile(
    "file", "filename.txt", "text/plain", "some json".getBytes()
  );
  mockMvc.perform(MockMvcRequestBuilders.multipart("/uploadFile").file(testInput))
      // .andExpect(status().isInternalServerError()).andExpect(content().string("Wrong
      // file format. Allowed: JSON."))
      .andDo(print());
}

By the way you don't need to explicitly add the default constructor and can remove the lines

/**
 * 
 */
public UploadTest()
{
    // default constructor
}

It is called default constructor because it is automatically there.

Stefan Birkner
  • 24,059
  • 12
  • 57
  • 72