Why is my Spring Boot autowired JPA Repository failing JUnit test?

My JUnit test is failing with the following error: "java.lang.IllegalArgumentException: Could not find field [userRepository] of type [null] on target [com.clearsoft.demo.UserResource@756b2d90]"



Here is the Test class:



@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = App.class)
@WebAppConfiguration
@IntegrationTest
public class UserResourceTest {

@Autowired
private UserRepository userRepository;

private MockMvc restUserMockMvc;

@Before
public void setup() {
UserResource userResource = new UserResource();
ReflectionTestUtils.setField(userResource, "userRepository", userRepository);
this.restUserMockMvc = MockMvcBuilders.standaloneSetup(userResource).build();
}

@Test
public void testGetExistingUser() throws Exception {
restUserMockMvc.perform(get("/user/foo")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"))
.andExpect(jsonPath("$.id").value(1));
}
}


And here is the UserResource service:



@RestController
public class UserResource {

@Autowired
UserRepository repository;

@RequestMapping(value = "/user/{email}", method = RequestMethod.GET)
User hello(@PathVariable("email") String email) {
return repository.findByEmailAddressAllIgnoringCase(email);
}

}


Here is the repository:



public interface UserRepository extends Repository<User, Long> {

Page<User> findAll(Pageable pageable);

User findByEmailAddressAllIgnoringCase(String emailAddress);

}


And here is the Entity:



@Entity
@Table(name = "users")
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = -1680480089065575997L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;

@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "email_address")
@Pattern(regexp = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\."
+ "[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@"
+ "(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9]"
+ "(?:[a-z0-9-]*[a-z0-9])?", message = "{invalid.email}")
private String emailAddress;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getEmailAddress() {
return emailAddress;
}

public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}

}


Here is the App class:



@SpringBootApplication
public class App implements CommandLineRunner {

@Autowired
UserRepository repository;

public static void main(String[] args) {
SpringApplication.run(App.class);
}

@Override
public void run(String... strings) throws Exception {
System.out.println("**************** RUN RUN RUN RUN");
for (User user : repository.findAll(null)) {
System.out.println("****************" + user.getEmailAddress());
}
}
}


Answers

Exception comes from this line:





ReflectionTestUtils.setField(userResource, "userRepository", userRepository);


Second parameter of setField method is a field name. UserResource has field "repository" - not "userRepository" as you try to set in your test.