How to create a mocked (by jmockit) spring bean? - spring

I am new to jmockit and would like to mock a bean inside my Java based Spring Application Configuration. I thought (better hoped) it would go like this:
#Configuration
public class MyApplicationConfig {
#Bean // this bean should be a mock
SomeService getSomeService() {
return new MockUp<SomeService>() {#Mock String someMethod() { return ""; }}.getMockInstance();
}
#Bean // some other bean that depends on the mocked service bean
MyApplication getMyApplication(SomeService someService) {
....
}
}
But unfortunatly this fails with "Invalid place to apply a mock-up".
I wonder if I can generate jmockit mocks inside Spring Configuration classes at all. I need the bean because it is referenced by other beans and the whole Spring Context initialization fails if I do not provide the mock as a Spring bean.
Thanks for any help.

Just use your regular Spring configuration. In a test class, declare the type to be mocked with #Capturing. It will mock whatever the implementation class that Spring used.
Edit: added full example code below.
import javax.inject.*;
public final class MyApplication {
private final String name;
#Inject private SomeService someService;
public MyApplication(String name) { this.name = name; }
public String doSomething() {
String something = someService.doSomething();
return name + ' ' + something;
}
}
public final class SomeService {
public String getName() { return null; }
public String doSomething() { throw new RuntimeException(); }
}
import org.springframework.context.annotation.*;
#Configuration
public class MyRealApplicationConfig {
#Bean
SomeService getSomeService() { return new SomeService(); }
#Bean
MyApplication getMyApplication(SomeService someService) {
String someName = someService.getName();
return new MyApplication(someName);
}
}
import javax.inject.*;
import org.junit.*;
import org.junit.runner.*;
import static org.junit.Assert.*;
import mockit.*;
import org.springframework.test.context.*;
import org.springframework.test.context.junit4.*;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = MyRealApplicationConfig.class)
public final class MyApplicationSpringTest {
#Inject MyApplication myApplication;
#Mocked SomeService mockService;
#BeforeClass // runs before Spring configuration
public static void setUpMocksForSpringConfiguration() {
new MockUp<SomeService>() {
#Mock String getName() { return "one"; }
};
}
#Test
public void doSomethingUsingMockedService() {
new Expectations() {{ mockService.doSomething(); result = "two"; }};
String result = myApplication.doSomething();
assertEquals("one two", result);
}
}
import org.junit.*;
import static org.junit.Assert.*;
import mockit.*;
// A simpler version of the test; no Spring.
public final class MyApplicationTest {
#Tested MyApplication myApplication;
#Injectable String name = "one";
#Injectable SomeService mockService;
#Test
public void doSomethingUsingMockedService() {
new Expectations() {{ mockService.doSomething(); result = "two"; }};
String result = myApplication.doSomething();
assertEquals("one two", result);
}
}

Spring-ReInject is designed to replace beans with mocks.

Related

Component bean is not injected (autowired) by spring when running AWS lambda function using Spring Cloud function AWS Adapter

I am implementing AWS lambda function creating handler using Spring Cloud function AWS Adapter SpringBootRequestHandler. The functional bean registered in GenericApplicationContext is invoked, but autowiring of component class is giving NullPointer Exception.
I have tried #ComponentScan for base package at Spring Application.
Application class:
#Slf4j
#SpringBootApplication
#ComponentScan({ "com.poc.evthub" })
#EnableConfigurationProperties(EventHubProperties.class)
public class EventHubServerlessApplication implements ApplicationContextInitializer<GenericApplicationContext> {
public EventHubServerlessApplication() {
}
public static void main(String[] args) throws Exception {
FunctionalSpringApplication.run(EventHubServerlessApplication.class, args);
}
#Bean
public KinesisEventFunction ingestEvents() {
return new KinesisEventFunction();
}
#Override
public void initialize(GenericApplicationContext context) {
log.debug("======== initialize ========");
context.registerBean("ingestEvents", FunctionRegistration.class,
() -> new FunctionRegistration<Function<KinesisEvent, ApiResponse>>(ingestEvents())
.type(FunctionType.from(KinesisEvent.class).to(ApiResponse.class).getType()));
}
}
Handler:
public class KinesisEventHandler extends SpringBootRequestHandler<KinesisEvent, ApiResponse> {
}
Functional Bean:
package com.poc.evthub.function;
import com.amazonaws.services.lambda.runtime.events.KinesisEvent;
import com.poc.evthub.beans.ApiResponse;
import com.poc.evthub.constant.Constants;
import com.poc.evthub.service.IngestionServiceFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.function.Function;
#Slf4j
#Component
public class KinesisEventFunction implements Function<KinesisEvent, ApiResponse> {
private Context context = null;
#Autowired
private IngestionServiceFactory ingestionServiceFactory;
#Autowired
#Qualifier("targetExecutionContext")
public void setContext(Context context) {
log.info("Context: {}", context);
this.context = context;
}
#Override
public ApiResponse apply(final KinesisEvent kinesisEvent) {
log.info("KinesisEventFunction apply called...");
String sourceDomain = System.getenv(Constants.SYSENV.SOURCE_DOMAIN);
log.info("Source Domain = {}", sourceDomain);
if(null == kinesisEvent || null == kinesisEvent.getRecords()) {
log.error("Event contains no data. {}", System.lineSeparator());
//TODO build response NOT FOUND
return null;
}
else
log.info("Received {} records from {}. {}",
kinesisEvent.getRecords().size(),
kinesisEvent.getRecords().get(0).getEventSourceARN(),
System.lineSeparator());
log.info("ingestionServiceFactory = {}",ingestionServiceFactory);
ingestionServiceFactory.ingest();
return null;
}
}
Complete code and pom is uploaded at:
https://github.com/rjavaria/eventhub-serverless
KinesisEventFunction apply called...
Also able to read the environment value(source_domain) from lambda, and receiving Kinesis event record.
But #Autowired ingestionServiceFactory is null. I am injecting this component bean to delegate the business logic.
What is missed here, so spring is not able to inject this component bean?
Thanks in advance!
You could try manual injection of your IngestionServiceFactory bean into your function class via constructor or setter injection.
In your function class, add a constructor and remove #Autowired from your IngestionServiceFactory field, like:
...
public class KinesisEventFunction implements Function<KinesisEvent, ApiResponse> {
...
// No #Autowired here...
private final IngestionServiceFactory ingestionServiceFactory;
...
// The new constructor here...
public KinesisEventFunction(final IngestionServiceFactory pIngestionServiceFactory) {
this.ingestionServiceFactory = pIngestionServiceFactory;
}
...
}
Then in your main class (the one implementing ApplicationContextInitializer<GenericApplicationContext>), pass the reference to the factory when registering the function bean, like:
...
public class EventHubServerlessApplication implements ApplicationContextInitializer<GenericApplicationContext> {
...
#Autowired
private IngestionServiceFactory ingestionServiceFactory;
...
#Bean
public KinesisEventFunction ingestEvents() {
return new KinesisEventFunction(this.ingestionServiceFactory);
}
#Override
public void initialize(GenericApplicationContext context) {
log.debug("======== initialize ========");
context.registerBean("ingestEvents", FunctionRegistration.class,
() -> new FunctionRegistration<>(ingestEvents())
.type(FunctionType.from(KinesisEvent.class).to(ApiResponse.class).getType()));
}
}
Or, as you are already manually registering the function bean with context.registerBean(), you could just:
...
public class EventHubServerlessApplication implements ApplicationContextInitializer<GenericApplicationContext> {
...
#Autowired
private IngestionServiceFactory ingestionServiceFactory;
...
#Override
public void initialize(GenericApplicationContext context) {
log.debug("======== initialize ========");
context.registerBean("ingestEvents", FunctionRegistration.class,
() -> new FunctionRegistration<>(new KinesisEventFunction(this.ingestionServiceFactory))
.type(FunctionType.from(KinesisEvent.class).to(ApiResponse.class).getType()));
}
}
Please, let me know if it works!

How to set and inject property Value from Test in Junit

My Service class uses a property set it application.properties
#Service
public class Service {
#Value("${my.prop}")
private String prop;
public Response doWork(String param1,String param2){
System.out.println(prop); //NULL!!!
}
}
I want to test it and set my own value:
Test Class:
#RunWith(MockitoJUnitRunner.class)
#TestPropertySource(locations = "application.properties",properties = { "my.prop=boo" })
public class ServiceUnitTest {
#InjectMocks
private Service service;
#Test
public void fooTest(){
Response re = service.doWork("boo", "foo");
}
}
But when I run the test, the value is null (not even the value that exists in application.properties).
I don't have experience with MockitoJUnitRunner, but I am able to achieve this using PowerMockRunner.
Set up the application context with a test configuration, and autowire the bean from you application context into your test class. You also shouldn't need the "locations = " bit in the #TestPropertySource annotation.
#RunWith(PowerMockRunner.class)
#PowerMockRunnerDelegate(SpringRunner.class)
#TestPropertySource(properties = { "my.prop=boo" })
public class ServiceUnitTest {
#TestConfiguration
static class ServiceUnitTestConfig {
#Bean
public Service service() {
return new Service();
}
}
#Autowired
Service service;
#Test
public void fooTest(){
Response re = service.doWork("boo", "foo");
}
}

Why are spring beans validated even if the condition says it should not be loaded into the context?

Given the example below, I would expect MyConfig.getSrvConfig() would not be called and therefore no validation would be executed on the returned object neither.
But for some reason the validation is executed and the test case fails. Is there anything wrong in this setup?
I know the test would pass if I have private MySrvConfigBean srvConfig not initialized at declaration - but I really don't want MySrvConfigBean to be a standalone class with a #ConfigurationProperties(prefix = "cfg.srvConfig") annotation.
#RunWith(SpringRunner.class)
#ContextConfiguration(classes = { TestCaseConfiguration.class })
public class ConditionalConfigValidationTest {
#Autowired
private ApplicationContext applicationContext;
#Test
public void test() {
assertNotNull(applicationContext);
assertFalse("srvConfig must NOT be in context", applicationContext.containsBean("srvConfig"));
}
#Configuration
#EnableConfigurationProperties(value = { MyConfig.class })
public static class TestCaseConfiguration {
}
#Component
#Validated
#ConfigurationProperties(prefix = "cfg")
public static class MyConfig {
private MySrvConfigBean srvConfig = new MySrvConfigBean();
#Bean
#Valid
#Conditional(MyCondition.class)
public MySrvConfigBean getSrvConfig() {
return srvConfig;
}
public static class MySrvConfigBean {
#NotNull
private String name;
public String getName() {
return name;
}
}
}
public static class MyCondition implements Condition {
#Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return false;
}
}
}
The reason we would like to have it this way is, because we then are able to structure configuration in code the same way as we have it in the YAML file, e.g.: (cfg and cfgA are the "root" objects for two different configuration hierarchies).
cfg:
srvConfig:
name: Dude
clientConfig:
xxx: true
yyy: Muster
cfgA:
aaaConfig:
bbb: false
ccc: Dundy
dddConfig:
fff: 3
It feels like the execution of the validation (triggered by #Valid on getSrvConfig()) is a bug in this case.
Apparently this is not supported and should be solved in a different way:
#Configuration
#Conditional(MyCondition.class)
#EnableConfigurationProperties(value = { MyConfig.class })
public static class TestCaseConfiguration {
}

Registering an instance as 'singleton' bean at application startup

I am playing around with Spring Boot and I am trying to construct an instance of ServiceImpl to be resolved when a Service is required. Currently I am annotating the implementation as #Component but this does not give me the chance to construct the instance as I want.
The ServiceImpl should be constructed with a String containing a path to a file on disk. I would like to do this in the main method of the #SpringBootApplication class of the application.
Maybe it's just me coming from a long .NET background where we usually setup the IoC container like:
Service service = new Service("C:\\data.bin");
container.RegisterSingleton<IService>(service); // now whoever asks for a IService will receive this precise instance
Does this make sense in Spring world?
LE: I am well aware of the GoF singleton definition (i.e. prevent everyone else from creating instances of the class) - I am not targeting this.
In the same file that you have #SpringBootApplication do:
#Bean
public IService service() {
return new Service("C:\\data.bin");
}
Spring should autowire everything up for you. It should be a singleton by default (http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/beans.html#beans-factory-scopes).
Edit 1: You should also annotate your Service implementation with #Service instead of #Component (see: What's the difference between #Component, #Repository & #Service annotations in Spring?).
Edit 2: You also don't necessarily have to put the #Bean method in the class that has #SpringBootApplication. You can put it in any class that has the #Configuration annotation.
As described in the comment this can be done by storing your location details on a configuration file and then inject them upon Spring Bean initialization.
Assuming your application.properties looks like this:
my.sample.config.A=somelocationA
my.sample.config.B=somelocationB
my.sample.config.C=somelocationC
my.sample.config.D.one=somelocationD1
my.sample.config.D.two=somelocationD2
Below I'm demo-ing 4 ways to do achieve this:
1.By injecting your property directly on the Bean method creation:
#Bean
public A myBeanA(#Value("${my.sample.config.A}") String myprop) {
System.out.println("from bean A with " + myprop);
return new A(myprop);
}
2.By injecting the property on a Config-wide variable and use that in your Bean method creation:
#Value("${my.sample.config.B}")
private String mylocationB;
//..
#Bean
public B myBeanB() {
System.out.println("from bean B with " + mylocationB);
return new B(mylocationB);
}
3.By injecting the whole environment in the Config and then hand-pick the property needed:
#Autowired
private Environment env;
//..
#Bean
public C myBeanC() {
String locationC = env.getProperty("my.sample.config.C");
System.out.println("from bean C with " + locationC);
return new C(locationC);
}
4.This is a Spring Boot exclusive way. You can use Type-safe Configuration Properties annotating with #ConfigurationProperties directly your bean defining a prefix-namespace and all params from that point onwards will be auto-mapped to the properties defined in that bean!
#ConfigurationProperties(prefix = "my.sample.config.D")
#Component
class D {
private String one;
private String two;
public String getOne() { return one; }
public void setOne(String one) {
System.out.println("from bean D with " + one);
this.one = one;
}
public String getTwo() { return two; }
public void setTwo(String two) {
System.out.println("from bean D with " + two);
this.two = two;
}
}
Below the overall one-file code in one piece:
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
#SpringBootApplication
public class DemoApplication {
#Autowired
private Environment env;
#Value("${my.sample.config.B}")
private String mylocationB;
#Bean
public A myBeanA(#Value("${my.sample.config.A}") String myprop) {
System.out.println("from bean A with " + myprop);
return new A(myprop);
}
#Bean
public B myBeanB() {
System.out.println("from bean B with " + mylocationB);
return new B(mylocationB);
}
#Bean
public C myBeanC() {
String locationC = env.getProperty("my.sample.config.C");
System.out.println("from bean C with " + locationC);
return new C(locationC);
}
#ConfigurationProperties(prefix = "my.sample.config.D")
#Component
class D {
private String one;
private String two;
public String getOne() { return one; }
public void setOne(String one) {
System.out.println("from bean D with " + one);
this.one = one;
}
public String getTwo() { return two; }
public void setTwo(String two) {
System.out.println("from bean D with " + two);
this.two = two;
}
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
class A {
private final String location;
public A(String location) { this.location = location; }
}
class B {
private final String location;
public B(String location) { this.location = location; }
}
class C {
private final String location;
public C(String location) { this.location = location; }
}
}

Arquillian and ejb mocking - legacy application

i'm working on legacy project (jee 5, jboss 4.2.3) and i need to write integration tests for this app. I was able to integrate arquillian remote module and run simple tests. But now i would like to mock some services in my ejb using mockito.
Example :
some ejb
#Local
public interface DummyService {
String welcomMessage();
}
#Stateless
#LocalBinding(jndiBinding = "ejb/DummyServiceBean/local")
public class DummyServiceBean implements DummyService {
#EJB(mappedName = "ejb/DummyServiceBean2/local")
private DummyService2 service;
#Override
public String welcomMessage() {
return "world!!!!" + " " + service.getSomething();
}
}
#Local
public interface DummyService2 {
String getSomething();
}
#Stateless
#LocalBinding(jndiBinding = "ejb/DummyServiceBean2/local")
public class DummyServiceBean2 implements DummyService2 {
#Override
public String getSomething() {
return "sth";
}
}
test class
#RunWith(Arquillian.class)
public class DummyServiceTest {
#EJB(mappedName = "ejb/DummyServiceBean/local")
private DummyService service;
#Mock
private DummyService2 service2;
#Deployment
public static Archive<?> createDeployment() {
final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class, "test.jar")
.addClasses(DummyService.class, DummyServiceBean.class,
DummyService2.class, DummyServiceBean2.class,
DummyServiceTest.class, InjectMocks.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
return ShrinkWrap.create(EnterpriseArchive.class, "test.ear")
.setApplicationXML(new File("application.xml"))
.addAsLibraries( // add maven resolve artifacts to the deployment
DependencyResolvers.use(MavenDependencyResolver.class)
.loadMetadataFromPom("pom.xml")
.artifact("org.mockito:mockito-all:1.9.5")
.resolveAs(GenericArchive.class))
.addAsModule(javaArchive);
}
#Before
public void setupMock() {
when(service2.getSomething()).thenReturn("qwerty");
}
#Test
public void should_assert_a_behaviour() {
System.out.println("Hello " + service.welcomMessage());
}
}
I can do this by not adding DummyServiceBean2.class into archive and by creating in test directory something like :
#Stateless
#LocalBinding(jndiBinding = "ejb/DummyServiceBean2/local")
public class MockDummyServiceBean2 implements DummyService2 {
#Override
public String getSomething() {
return "mock sth";
}
}
but this is bad practice. I got the idea to swap during runtime DummyServiceBean2 proxy reference using reflection in DummyServiceBean class for a new one with InvocationHandler which use mock inside his invoke method but it ended up on an exception
IllegalArgumentException: Can not set com.example.DummyService2 field com.example.DummyServiceBean.service to com.sun.proxy.$Proxy71
Any ideas how can i swap/replace DummyServiceBean2 proxy for new one or how can i replace invocation handler in existing one ?

Resources