Chen Jian's Java Blog 

In typescript: use Jest mock a function from a module, and reference this mocked function

by Chen Jian


Posted on 2019-07-25 12:00 in Frontend


import * as someModule from "../SomeModule"; test("xxx", ()=>{ const mockedModule = someModule as jest.Mocked < typeof someModule > ; mockedModule.someFunction = jest.fn(); ... //reference the mocked function ...


Jetty's start.jar forks a new JVM to run the app if logback module is enabled

by Chen Jian


Posted on 2019-07-17 12:00 in Java


Jetty is too smart. If there is any JVM option provided, Jetty will spawn a new JVM instance to do the real work, even if you don't use "--exec" option in start.ini And jetty's logback module introduces a JVM option. See here You will see: ...


Miscellaneous tips while developing in WSL (Windows Subsystem for Linux)

by Chen Jian


Posted on 2019-07-16 12:00 in Dev Tools


File change monitoring doesn't work, such as the auto-refresh of create-react-app Use the following before your "npm start" or put it on env file: CHOKIDAR_USEPOLLING=true And if you are using Intellij, you need to...


Change the font family globally in Material-UI 3.x

by Chen Jian


Posted on 2019-06-29 12:00 in Frontend


Some say you should use the jsx-global-plugin like "@global body{fontFamily:'someFont'}" , but it doesn't work in my project. The global CSS still has the lower CSS speciality than MUI components' CSS because this global CSS is using a type selector. ...


Code snippet: Swagger + Spring Boot

by Chen Jian


Posted on 2019-06-25 12:00 in Java


compile 'io.springfox:springfox-swagger2:2.9.2' compile 'io.springfox:springfox-swagger-ui:2.9.2' /** * See https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api */ @Configuration @EnableSwagger2 public class...


Generate PDF from server side using Java

by Chen Jian


Posted on 2019-06-21 12:00 in Java


You can use OpenPDF to draw a PDF (the open source fork of iText). However, the learning curve of creating PDF with layouts and stuff is very high. A more cost-effective solution is create an HTML version of it and convert it using Flying Saucer ...


Redux + Typescript: The type of the root state

by Chen Jian


Posted on 2019-06-14 12:00 in Frontend


You can get the type of the state from the root reducer // the reducer const appReducer = combineReducers({ global: globalStateReducer, example: exampleReducer }); type AppState = ReturnType<typeof appReducer> However the...


Redux + Typescript: Use class to define Actions

by Chen Jian


Posted on 2019-06-14 12:00 in Frontend


You want the actions to be typed. You should use interface or class. Class is better than interface because in interfaces you cannot define the action "type" as a constant, but in classes you can: class LoadingStartedAction implements...


Read process output in Java without hanging the main thread

by Chen Jian


Posted on 2019-06-14 12:00 in Java


See https://github.com/chenjianjx/wsdl2html/blob/541a33820e9f246a6a73aafaadbb1cf96c6123fc/src/main/java/org/jaxws/util/os/SystemProcessUtils.java


Gradle upload a custom-generated jar file to Nexus

by Chen Jian


Posted on 2019-06-14 12:00 in Java


Do this: apply plugin: 'maven-publish' publishing { publications { maven( MavenPublication ) { artifact theTaskThatGeneratesJar } } repositories { maven { ...


Don't run Jetty with the default "bin/jetty.sh start"

by Chen Jian


Posted on 2019-06-13 12:00 in Java


Instead, run it with: java -jar start.jar "bin/jetty.sh start" does a lot of doggy things for you. You will get suprising results if you use it, for example, the logs inside your war are not shown. Avoid it unless you do know about those...


Sending html emails with images inside

by Chen Jian


Posted on 2019-05-26 12:00 in Java


If the images are external image links that can be accessed by http url, then nothing speical needs to be done. If the images are external image files, then you have 2 choices. Embed the images as base64 strings. . This is simple....


React Component: A pair of componentDidMount() and componentWillUnmount() may cause infinite loop if error is thrown from render

by Chen Jian


Posted on 2019-04-18 12:00 in Frontend


The following will cause an infinite loop. The render() method will be called again and again. class ToDoItemEditComponentContainer extends React.Component { componentDidMount() { this.props.loadItem(); //load item remotely and...


JSON.stringify() won't work for ES6 Set

by Chen Jian


Posted on 2019-04-17 12:00 in Frontend


It will just return "{}", no matter if the Set is empty. This also means deep clone based on JSON won't work. If JSON.stringify() matters to you, use Array instead of Set


Material-UI: avoid RadioGroup if there are complex elements inside each radio

by Chen Jian


Posted on 2019-03-03 12:00 in Frontend


Just use seperate Radios The problem with RadioGroup with nested elements under Radios is that it messes up with your layout. RadioGroup is a child of FormGroup, which does a lot of styling itself. If you use it, you will lose a lot of freedom...