Skip to content

Commit acd6e3c

Browse files
committed
Update docs, add preample, organize refactor, rework code to conform to junit5 standards
1 parent 3782bd5 commit acd6e3c

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

README.md

+30-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
# Mockito Object Injection
22

3-
Inject Strings (or other objects) into your `@InjectMocks` targets [objects under test] without booting a Spring, Weld, CDI, Arquillian, EJB, or other container. Super lightweight and easy to use. Skip straight to Examples and Usage if you know what you're looking for.
3+
## Preamble
44

5-
## Problem
5+
* Mock testing leads to highly focused tests where every variable is controlled.
6+
* Dependency Injection + well structured programs + Mock testing = clean code codebase.
7+
8+
The problem is, how does one Mock a String, Boolean, or other final type if those types are being Injected?
9+
10+
## Summary
11+
12+
This Junit Extension allows you to inject Strings (or any other object) into your `@InjectMocks` targets [objects under test] without booting a Spring, Weld, CDI, Arquillian, EJB, or other container. It's super lightweight, over 100,000x faster than Arquillian, and easy to use.
13+
14+
## Example Problem
615

716
Take this Spring Controller (or if you're using the far superior and modern CDI framework, think `@AppplicationScoped` instead of `@Controller` and `@Inject` instead of `@Autowired`)
817

@@ -30,15 +39,16 @@ If you wanted to write a _true unit test*_ with no external dependencies, you'd
3039

3140
```
3241
@ExtendWith({ MockitoExtension.class })
33-
public class MyControllerTest {
42+
class MyControllerTest {
3443
@InjectMocks
3544
private MyController myController;
3645
@Mock
3746
private Logger log;
3847
@Mock
3948
private Authenticator auther;
4049
41-
public void testDoSomething() throws Exception {
50+
@Test
51+
void testDoSomething() throws Exception {
4252
myController.doSomething();
4353
// results in NPE because myController.securityEnabled is null
4454
}
@@ -47,7 +57,7 @@ public class MyControllerTest {
4757

4858
* Testing a "unit" of code is a unit test. In Java, typically a class is the smallest unit of code.
4959

50-
## Examples
60+
## Example Solution
5161

5262
This JUnit5 extension allows you to arbitrarily set any field on your `@InjectMocks` [class under test] target. The injections happen _very late_; they happen when you call any non-private method on the class under test.
5363

@@ -58,7 +68,7 @@ import com.github.exabrial.junit5.injectmap.InjectMapExtension;
5868
5969
@TestInstance(Lifecycle.PER_CLASS)
6070
@ExtendWith({ MockitoExtension.class, InjectExtension.class })
61-
public class MyControllerTest {
71+
class MyControllerTest {
6272
@InjectMocks
6373
private MyController myController;
6474
@Mock
@@ -68,21 +78,23 @@ public class MyControllerTest {
6878
@InjectionSource
6979
private Boolean securityEnabled;
7080
71-
public void testDoSomething_secEnabled() throws Exception {
81+
@Test
82+
void testDoSomething_secEnabled() throws Exception {
7283
securityEnabled = Boolean.TRUE;
7384
myController.doSomething();
7485
// wahoo no NPE! Test the "if then" half of the branch
7586
}
76-
77-
public void testDoSomething_secDisabled() throws Exception {
87+
88+
@Test
89+
void testDoSomething_secDisabled() throws Exception {
7890
securityEnabled = Boolean.FALSE;
7991
myController.doSomething();
8092
// wahoo no NPE! Test the "if else" half of branch
8193
}
8294
}
8395
```
8496

85-
## PostConstruct invocation
97+
## @PostConstruct invocation
8698

8799
CDI and SpringFramework allow the use of `@PostConstruct`. This is like a constructor, except the method annotated will be invoked _after_ dependency injection is complete. This extension can be commanded to invoke the method annotated with `@PostConstruct` like so:
88100

@@ -102,9 +114,17 @@ public class MyController {
102114
```
103115

104116
```
117+
@TestInstance(Lifecycle.PER_CLASS)
118+
@ExtendWith({ MockitoExtension.class, InjectExtension.class })
119+
class MyControllerTest {
120+
105121
@InjectMocks
106122
@InvokePostConstruct
123+
// The postConstruct on this controller will be invoked
107124
private MyController myController;
125+
126+
@Test
127+
void testSometing()
108128
```
109129

110130
Ref: https://docs.oracle.com/javaee/7/api/javax/annotation/PostConstruct.html

0 commit comments

Comments
 (0)