Spring Interceptor

Function Can constitute a stack of interceptors to complete specific functions, such as logging, login judgment, permission check and so on. Benefits Interceptors also allow you to modularize generic code as reusable classes. Applications of interceptors: AOP; needs some business logic (need to inject beans, etc.) Understand An interceptor acts Read more…

Java 8 Stream API Common Operations

foreach list.forEach(System.out::println); map List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5); List<Integer> squaresList = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList()); filter List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl"); // count empty strings long count = strings.stream().filter(string -> string.isEmpty()).count(); List<UserInfo> list = new ArrayList<>(); UserInfo userInfo1 = new UserInfo(); userInfo1.setUserName("aa"); Read more…

Jackson: Basic Use

Basic JavaBean we use: public class AccountBean { private int id; private String name; private String email; private String address; private Birthday birthday; //getter、setter @Override public String toString() { return this.name + "#" + this.id + "#" + this.address + "#" + this.birthday + "#" + this.email; } } public Read more…

Jackson vs Gson

1. Introduction In this article, we’ll compare the Gson and Jackson APIs for serializing and deserializing JSON data to Java objects and vice-versa. Gson and Jackson are complete libraries offering JSON data-binding support for Java. Each are actively developed open-source projects which offer to handle of complex data types and support for Java Generics. Read more…

What is the difference between SpringJUnit4ClassRunner and SpringRunner

SpringRunner is an alias for the SpringJUnit4ClassRunner. @RunWith(SpringRunner.class) tells JUnit to run using Spring’s testing support. SpringRunner is the new name for SpringJUnit4ClassRunner, it’s just a bit easier on the eye. SpringRunner is only available after spring-test 4.3. SpringRunner class extends SpringJUnit4ClassRunner.   Read the doc source code package org.springframework.test.context.junit4; import org.junit.runners.model.InitializationError; public final class SpringRunner extends SpringJUnit4ClassRunner Read more…

@RunWith(SpringJUnit4ClassRunner.class)

In order for the unit test to run a batch job, the framework must load the job’s ApplicationContext. Two annotations are used to trigger this: @RunWith(SpringJUnit4ClassRunner.class): Indicates that the class should use Spring’s JUnit facilities. @ContextConfiguration(locations = {…}): Indicates which XML files contain the ApplicationContext. For example: import org.junit.Test; import Read more…

A Template of JUnit5 with Mockito

@ExtendWith(MockitoExtension.class) class ArticleManagerTest { @Mock ArticleDatabase database; @Mock User user; @Spy @InjectMocks private ArticleManager manager; @BeforeEach void setup(){ // some initialization codes } @Test void shouldDoSomething() { // TODO perform some tests with this managers when(database.some(param)).thenReturn(data); Object res = manager.exe(); assertEquals(res, "data"); verify(database).some(param); } } Reading more: Unit tests with Read more…

Catalogue