Build React Native App (4) - Redux, Jest, and NativeBase

Image
From this blog, typescript feature will be added. There are couple of ways to implement static type checking like; flow from facebook, PropTypes and Typescript. Typescript is well integrated with Visual Studio Code and supports better linter, error messages, and intellisense. Reference site Github Sample Ex4 Currnet version D:\GitRepo\reactnative>npm --version 6.3.0 D:\GitRepo\reactnative>react-native --version react-native-cli: 2.0.1 react-native: n/a - not inside a React Native project directory D:\GitRepo\reactnative>yarn --version 1.9.4 Creating React Native App $ react-native init ex4 If you want to specify the version, add "--version 0.57.3" at the end. Add NativeBase to React Native $ npm install native-base --save ... + native-base@2.8.1 added 71 packages from 42 contributors, removed 50 packages, updated 829 packages and audited 34989 packages in 138.542s found 0 vulnerabilities $ $ yarn yarn install v1.9.4 warning package-lock.json found. You

Developing SPA app with AngularJS, Spring, Spring Boot, REST, and MyBatis - 2. Unit Testing

Developing SPA app with AngularJS, Spring, SpringBoot, REST, and MyBatis - 2. Unit Testing

Introduction

From the first posting, we just created simple application and run the application using maven and SpringBoot. From this tutorial, we will try to figure out how to do the unit testing with SpringBoot

Recommend Syntax and UI for atom

Folder structure

root/pom.xml
root/src/main/java/AngularSpring.java
root/src/main/test/AngularSpringTest.java

Setting up the Unit Testing

When you add spring-boot-starter-test into the POM file, following 4 libraries will be added;
  • Spring Test — integration test support for Spring applications.
  • JUnit — The de-facto standard for unit testing Java applications.
  • Hamcrest — A library of matcher objects (also known as constraints or predicates) allowing assertThat style JUnit assertions.
  • Mockito — A Java mocking framework.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.alexjoh</groupId>
    <artifactId>angularspring</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.0.RELEASE</version>
    </parent>
    <!-- Additional lines to be added here... -->

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

</project>

Writing Test Case for the Main Class from Previous example

From this example, we will do the Web Result Unit test from the main class.
The main class is the same from the first example.
.\src\test\AngularSpringTest.java
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

//import static org.hamcrest.Matchers.hasSize;
//import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
//import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
//import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


@RunWith(SpringJUnit4ClassRunner.class) // this package comes from or.junit.runner.RunWith
@SpringApplicationConfiguration(classes = MockServletContext.class)
@WebAppConfiguration
public class AngularSpringTest {
  final String BASE_URL = "http://localhost:8080/";

  private MockMvc mockMvc;

  @Before
  public void setUp() {
    this.mockMvc = MockMvcBuilders.standaloneSetup(new AngularSpring()).build();

    //this.mockMvc = MockMvcBuilders.<StandaloneMockMvcBuilder>webAppContextSetup(new AngularSpring()).build();
    //this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
  }

  @Test
  public void checkDefaultString () throws Exception {
    mockMvc.perform(get("/"))
      .andExpect(status().isOk())
      .andExpect(content().string("Welcome to Angular and Spring Testing!!"));
  }
}
Let’s look at each line from the code
  • @RunWith(SpringJUnit4ClassRunner.class) // this package comes from or.junit.runner.RunWith
    This line will indicate that this unit test will be run with SpringJUnit4ClassRunner program
  • @SpringApplicationConfiguration(classes = MockServletContext.class)
  • @WebAppConfiguration
Create main class using MockMvcBuilders
  • this.mockMvc = MockMvcBuilders.standaloneSetup(new AngularSpring()).build();
Testing REST result
  • mockMvc.perform(get(“/”)): This will fetch data from the main class
  • .andExpect(status().isOk()): This will examine the header of HTTP and check whether there is no error or not
  • .andExpect(content().string(“Welcome to Angular and Spring Testing!”));: This will check the content of data and will check whether the result string is the same or not. If the string is not the same, mvn will generate the test failure message

Compile result

After running mvn package from the terminal (shortcut: ctrl+` ), you will see the test result is passed.
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.5:jar (default-jar) @ angularspring ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.774 s
[INFO] Finished at: 2015-12-19T21:03:52-07:00
[INFO] Final Memory: 21M/308M
[INFO] ------------------------------------------------------------------------
PS C:\Users\alex\OneDrive for Business\Programming\SpringBoot\AngularSpring
To test whether this test routine catches the failure, tested with
Failed tests:
  AngularSpringTest.checkDefaultString:47 Response content expected:<Welcome to Angular and Spring Testing!!> but was:<Welcome to Angular and Spr
ing Testing!>

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.505 s
[INFO] Finished at: 2015-12-19T20:22:50-07:00
[INFO] Final Memory: 21M/308M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test (default-test) on project angularspring: There are test
 failures.
[ERROR]
[ERROR] Please refer to C:\Users\alex\OneDrive for Business\Programming\SpringBoot\AngularSpring\target\surefire-reports for the individual test
results.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Conclusion

From this sample, we run the Unit Testing to the REST result. Unit Testing is another huge area to learn and as I learn more, I will keep explain in detail. TDD(Test Driven Development) and Design Pattern is must-have knowledge from the programming world. If you are not sure what it is, please do your own diligent.
Written with StackEdit.

Comments

Popular posts from this blog

Export folder structure to file on Windows, Mac, and Linux

Build React Native App (4) - Redux, Jest, and NativeBase

Replacing text in PDF file using iTextSharp