update source code to sync with article
This commit is contained in:
parent
6b21f217ed
commit
0f851a1d84
|
@ -86,6 +86,15 @@ public class OptionalUnitTest {
|
||||||
Optional<String> opt = Optional.ofNullable(null);
|
Optional<String> opt = Optional.ofNullable(null);
|
||||||
String name = opt.get();
|
String name = opt.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAnEmptyOptional_thenIsEmptyBehavesAsExpected() {
|
||||||
|
Optional<String> opt = Optional.of("Baeldung");
|
||||||
|
assertTrue(opt.isPresent());
|
||||||
|
|
||||||
|
opt = Optional.ofNullable(null);
|
||||||
|
assertFalse(opt.isPresent());
|
||||||
|
}
|
||||||
|
|
||||||
// Conditional Return With filter()
|
// Conditional Return With filter()
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -14,11 +14,11 @@
|
||||||
<relativePath>../../parent-boot-2</relativePath>
|
<relativePath>../../parent-boot-2</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<modules>
|
<!--<modules>
|
||||||
<module>disabling-console-jul</module>
|
<module>disabling-console-jul</module>
|
||||||
<module>disabling-console-log4j2</module>
|
<module>disabling-console-log4j2</module>
|
||||||
<module>disabling-console-logback</module>
|
<module>disabling-console-logback</module>
|
||||||
</modules>
|
</modules>-->
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
|
|
|
@ -1,43 +1,22 @@
|
||||||
package com.baeldung.web.log.app;
|
package com.baeldung.web.log.app;
|
||||||
|
|
||||||
import javax.servlet.ServletContext;
|
|
||||||
import javax.servlet.ServletException;
|
|
||||||
import javax.servlet.ServletRegistration;
|
import javax.servlet.ServletRegistration;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.web.context.ContextLoaderListener;
|
|
||||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
|
||||||
import org.springframework.web.servlet.DispatcherServlet;
|
|
||||||
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
|
||||||
import com.baeldung.web.log.config.CustomeRequestLoggingFilter;
|
import com.baeldung.web.log.config.CustomeRequestLoggingFilter;
|
||||||
|
|
||||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
|
||||||
|
|
||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
@ComponentScan("com.baeldung.web.log")
|
@ComponentScan("com.baeldung.web.log")
|
||||||
@PropertySource("application-log.properties")
|
@PropertySource("application-log.properties")
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class Application extends SpringBootServletInitializer {
|
public class Application {
|
||||||
|
|
||||||
public static void main(final String[] args) {
|
public static void main(final String[] args) {
|
||||||
SpringApplication.run(Application.class, args);
|
SpringApplication.run(Application.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onStartup(ServletContext container) throws ServletException {
|
|
||||||
|
|
||||||
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
|
|
||||||
context.setConfigLocation("com.baeldung.web.log");
|
|
||||||
container.addListener(new ContextLoaderListener(context));
|
|
||||||
|
|
||||||
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(context));
|
|
||||||
dispatcher.setLoadOnStartup(1);
|
|
||||||
dispatcher.addMapping("/");
|
|
||||||
|
|
||||||
container.addFilter("customRequestLoggingFilter", CustomeRequestLoggingFilter.class).addMappingForServletNames(null, false, "dispatcher");
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
package com.baeldung.web.log.config;
|
||||||
|
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import org.springframework.web.context.ContextLoaderListener;
|
||||||
|
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||||
|
import org.springframework.web.servlet.DispatcherServlet;
|
||||||
|
import org.springframework.web.WebApplicationInitializer;
|
||||||
|
|
||||||
|
public class CustomWebAppInitializer implements WebApplicationInitializer {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStartup(ServletContext container) throws ServletException {
|
||||||
|
|
||||||
|
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
|
||||||
|
context.setConfigLocation("com.baeldung.web.log");
|
||||||
|
container.addListener(new ContextLoaderListener(context));
|
||||||
|
|
||||||
|
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(context));
|
||||||
|
dispatcher.setLoadOnStartup(1);
|
||||||
|
dispatcher.addMapping("/");
|
||||||
|
|
||||||
|
container.addFilter("customRequestLoggingFilter", CustomeRequestLoggingFilter.class).addMappingForServletNames(null, false, "dispatcher");
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,15 +6,12 @@ import javax.servlet.ServletContext;
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.ServletRegistration;
|
import javax.servlet.ServletRegistration;
|
||||||
|
|
||||||
import org.springframework.context.support.GenericApplicationContext;
|
|
||||||
import org.springframework.web.WebApplicationInitializer;
|
import org.springframework.web.WebApplicationInitializer;
|
||||||
import org.springframework.web.context.ContextLoaderListener;
|
import org.springframework.web.context.ContextLoaderListener;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
|
||||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||||
import org.springframework.web.context.support.GenericWebApplicationContext;
|
|
||||||
import org.springframework.web.servlet.DispatcherServlet;
|
import org.springframework.web.servlet.DispatcherServlet;
|
||||||
|
|
||||||
public class MainWebAppInitializer implements WebApplicationInitializer
|
public class MyWebApplicationInitializer implements WebApplicationInitializer
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -22,7 +19,7 @@ public class MainWebAppInitializer implements WebApplicationInitializer
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onStartup(final ServletContext sc) throws ServletException {
|
public void onStartup(final ServletContext sc) throws ServletException {
|
||||||
System.out.println("MainWebAppInitializer.onStartup()");
|
System.out.println("MyWebApplicationInitializer.onStartup()");
|
||||||
|
|
||||||
// Create the 'root' Spring application context
|
// Create the 'root' Spring application context
|
||||||
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
|
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
|
|
@ -16,7 +16,7 @@ public class SpringProfilesWithMavenPropertiesIntegrationTest {
|
||||||
DatasourceConfig datasourceConfig;
|
DatasourceConfig datasourceConfig;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSpringProfiles() {
|
public void setupDatasource() {
|
||||||
Assert.assertTrue(datasourceConfig instanceof DevDatasourceConfig);
|
Assert.assertTrue(datasourceConfig instanceof DevDatasourceConfig);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -75,7 +75,53 @@ public class MockitoAnnotationUnitTest {
|
||||||
Mockito.doReturn(100).when(spiedList).size();
|
Mockito.doReturn(100).when(spiedList).size();
|
||||||
assertEquals(100, spiedList.size());
|
assertEquals(100, spiedList.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSpyingOnList_thenCorrect() {
|
||||||
|
List<String> list = new ArrayList<String>();
|
||||||
|
List<String> spyList = Mockito.spy(list);
|
||||||
|
|
||||||
|
spyList.add("one");
|
||||||
|
spyList.add("two");
|
||||||
|
|
||||||
|
Mockito.verify(spyList).add("one");
|
||||||
|
Mockito.verify(spyList).add("two");
|
||||||
|
|
||||||
|
assertEquals(2, spyList.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingTheSpyAnnotation_thenObjectIsSpied() {
|
||||||
|
spiedList.add("one");
|
||||||
|
spiedList.add("two");
|
||||||
|
|
||||||
|
Mockito.verify(spiedList).add("one");
|
||||||
|
Mockito.verify(spiedList).add("two");
|
||||||
|
|
||||||
|
assertEquals(2, spiedList.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenStubASpy_thenStubbed() {
|
||||||
|
List<String> list = new ArrayList<String>();
|
||||||
|
List<String> spyList = Mockito.spy(list);
|
||||||
|
|
||||||
|
assertEquals(0, spyList.size());
|
||||||
|
|
||||||
|
Mockito.doReturn(100).when(spyList).size();
|
||||||
|
assertEquals(100, spyList.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCreateSpy_thenCreate() {
|
||||||
|
List<String> spyList = Mockito.spy(new ArrayList<>());
|
||||||
|
|
||||||
|
spyList.add("one");
|
||||||
|
Mockito.verify(spyList).add("one");
|
||||||
|
|
||||||
|
assertEquals(1, spyList.size());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenNotUseCaptorAnnotation_thenCorrect() {
|
public void whenNotUseCaptorAnnotation_thenCorrect() {
|
||||||
final List<String> mockList = Mockito.mock(List.class);
|
final List<String> mockList = Mockito.mock(List.class);
|
||||||
|
|
Loading…
Reference in New Issue