@Test(expectedExceptions = IllegalArgumentException.class) public void shouldThrowExceptionWhenNullConfig() throws ServletException { // given // when testFilter.init(null); // then }
@Test(expectedExceptions = ServletException.class) public void shouldThrowExceptionWhenNoConfig() throws ServletException { // given FilterConfig config = mock(FilterConfig.class); // when testFilter.init(config); // then }
@Test(expectedExceptions = ServletException.class) public void shouldThrowExceptionWhenNoOriginInConfig() throws ServletException { // given FilterConfig config = mock(FilterConfig.class); given(config.getInitParameter(CORSConstants.METHODS_KEY)).willReturn("GET,POST"); // when testFilter.init(config); // then }
@Test(expectedExceptions = ServletException.class) public void shoudlThrowExceptionWhenNoMethodsInConfig() throws ServletException { // given FilterConfig config = mock(FilterConfig.class); given(config.getInitParameter(CORSConstants.ORIGINS_KEY)).willReturn("www.google.com"); // when testFilter.init(config); // then }
@Test(expectedExceptions = ServletException.class) public void shouldThrowExceptionWhenMaxAgeIsNaN() throws ServletException { // given FilterConfig config = mock(FilterConfig.class); given(config.getInitParameter(CORSConstants.ORIGINS_KEY)).willReturn("www.google.com"); given(config.getInitParameter(CORSConstants.METHODS_KEY)).willReturn("GET,POST"); given(config.getInitParameter(CORSConstants.MAX_AGE_KEY)).willReturn("words"); // when testFilter.init(config); // then }
@Test public void shouldConfigureWithBothMethodsAndOriginsNoErrors() { // given FilterConfig config = mock(FilterConfig.class); given(config.getInitParameter(CORSConstants.ORIGINS_KEY)).willReturn("www.google.com"); given(config.getInitParameter(CORSConstants.METHODS_KEY)).willReturn("GET,POST"); boolean success = true; // when try { testFilter.init(config); } catch (ServletException e) { success = false; } // then assertTrue(success); }