Monthly Archives: April 2021

Fixing angular 404 page not found error on page refresh

By default, navigation within an Angular application relies on PathLocationStrategy, which uses HTML5 history.pushState, a technique that changes a browser’s location and history without sending an HTTP request to the server. This is sweet because your application behaves like any web site, while being way more reactive since it skips the browser/server communications.

However, there’s one major drawback, if you refresh your page after visiting an Angular deep link, or if you access it using your favourites or history, your browser will send the request to the server, which will have no hint about the requested resource, and will return a 404 error.

To prevent this, you need to set some server side configuration, which will depend on your web server. Here are the solutions for Apache and Tomcat, on a Debian 10.

Apache

The easiest way is to include a .htaccess, in your Angular application, and to enable Apache rewrite mod

1. Install Apache rewrite mod

sudo a2enmod rewrite
sudo systemctl restart apache

Then add .htaccess in your Angular app folder:

RewriteEngine On
 RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
 RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
 RewriteRule ^ - [L]
 RewriteRule ^ /myAngularAppBase

If you ship the .htaccess with your app, don’t forget to include it in the assets section of your angular.json file

{
...
"assets": [
              "src/.htaccess",
              "src/favicon.ico",
              "src/assets",
              {
                "glob": "**/*",
                "input": "./node_modules/@ant-design/icons-angular/src/inline-svg/",
                "output": "/assets/"
              }
...
}

Tomcat

1. enable the rewrite valve in /opt/tomcat/conf/context.xml

 <!-- REWRITE VALVE -->  <Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />  <!-- // -->  

2. add rewrite.config file in the project WEB-INF folder with below content such as on /opt/tomcat/webapps/myProject/WEB-INF/rewrite.config.

RewriteCond %{REQUEST_URI} !^.*\.(bmp|css|gif|htc|html?|ico|jpe?g|js|pdf|png|swf|txt|xml|svg|eot|woff|woff2|ttf|map)$ RewriteRule ^(.*)$ /index.html [L]

If you ship it with your Angular app, don’t forget to add WEB-INF folder to your angular.json assets section.

{
...
"assets": [
              "src/WEB-INF",
              "src/favicon.ico",
              "src/assets",
              {
                "glob": "**/*",
                "input": "./node_modules/@ant-design/icons-angular/src/inline-svg/",
                "output": "/assets/"
              }
...
}

Fixing Mockito UnnecessaryStubbingException with JUnit5

After upgrading to JUnit 5, you may bump into some unit tests now failing with the following error :

org.mockito.exceptions.misusing.UnnecessaryStubbingException: 
Unnecessary stubbings detected.
Clean & maintainable test code requires zero unnecessary code.
Following stubbings are unnecessary (click to navigate to relevant line of code):...

JUnit 5 is tricter regarding stubbing. As explained in the error message, unnecessary stubbing is not a good practice. It usually results from copy and paste, and creates confusion about the tested behaviour. In such a situation, you just want to remove the unnecessary stubbing.

However, there are some cases when you’d like a less strict approach. For example, when you use a helper method that does some mocking that is required by most of the cases, but not all. Than you have to choices :

  • instead of mocking directly with “when(…).then(…)”, prefix the call with lenient(). Ex
    lenient().when(oauth.getAuthorities()).thenReturn(authorities);
  • else, annotate your test class with :
    @MockitoSettings(strictness = Strictness.LENIENT)
    It will relax the mockito stubbing rules.

Increasing uploaded files maximum size in a Spring Boot 2 application

When uploading a file to a SpringBoot Application, you will get the following exception for files over 1 Mb size :

Maximum upload size exceeded; nestedexception is java.lang.IllegalStateException:org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException: The field fileexceeds its maximum permitted size of1048576 bytes.

These are the default Spring Boot limits. To increase these limits to 10 Mb, with Spring Boot 2, add the following lines to your application.properties file:

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB