Step 1: Create a simple Maven project from a command-line. Just press enter for all the prompts.
1 |
mvn archetype:generate -DgroupId=com.mytutorial -DartifactId=simple-mocking-test |
Step 2: Import the above “simple-mocking-test” in the file system into eclipse. File –> Import –> “Existing Maven Projects” and then select the “simple-mocking-test” folder that was created by the “Step 1” from the file system.
Step 3: Add the testing, mocking, and Spring libraries to the pom.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
<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.mytutorial</groupId> <artifactId>simple-mocking-test</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>simple-mocking-test</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <mockito-all.version>1.9.0</mockito-all.version> <spring.version>3.2.13.RELEASE</spring.version> </properties> <dependencies> <!-- JUnit Library --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- Mockito Library (for Mocking)--> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>${mockito-all.version}</version> <scope>test</scope> </dependency> <!-- Dependnecy Injection Annotation @Named, @Inject--> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency> <!-- Spring Dependencies (core libraries)--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring testing annotations like @ContextConfiguration, @RunWith, etc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> </dependencies> </project> |
Step 4: Service and DAO layer interfaces and implementations
Service Layer
1 2 3 4 5 |
package com.mytutorial; public interface SimpleService { abstract String processUser(int id); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.mytutorial; import javax.inject.Inject; import javax.inject.Named; @Named public class SimpleServiceImpl implements SimpleService { @Inject SimpleDao dao; public String processUser(int id) { String name = dao.getNameById(id); return "Hello " + name; } } |
DAO Layer
1 2 3 4 5 |
package com.mytutorial; public interface SimpleDao { abstract String getNameById(int id); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.mytutorial; import javax.inject.Named; @Named public class SimpleDaoImpl implements SimpleDao { public String getNameById(int id) { if(id == 1) { return "John"; } else if (id == 2) { return "Peter"; } else { throw new IllegalArgumentException(); } } } |
Step 5: Wiring the DI via Java config.
1 2 3 4 5 6 7 8 9 10 |
package com.mytutorial; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan(basePackages={"com.mytutorial"}) public class AppConfig { } |
Step 6: The JUnit test class. This is more of a JUnit integration testing than unit testing as the test spans service and dao layers. In Part 2, I will be mocking the Dao layer with the Mockito framework
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.mytutorial; import javax.inject.Inject; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @ContextConfiguration(classes = { AppConfig.class }) @RunWith(SpringJUnit4ClassRunner.class) public class SimpleTest { @Inject SimpleService service; @Test public void testProcessUser(){ String processUser = service.processUser(1); Assert.assertEquals("Hello John", processUser); } } |
Step 7: Run as JUnit test.
(Visited 6 times, 1 visits today)