Calling a static method on a variable rather than class - java

So I am writing some Scala tests for some Java code (I am very new at both of them). What I am trying to do is instead of calling .create on the Configuration class, I want to call create on the conf mock object. How can I go about doing this?
When I try to call .create on a mock of the Configuration class, I get an error since (I think) .create is a static method from a custom abstract Configuration java class
Scala test code:
var conf: Configuration = mock[Configuration]
when(Configuration.create("var1","var2", "var3")).thenReturn(testConfiguration)
Thanks!

Related

How to Mock Eclipse Plugin class with methods?

I have to write a JUnit test case for a method which accepts Plugin instance.
Inside the method, I have to get the Plugin Symbolic name using
plugin.getBundle().getSymbolicName()
I have tried to make a Plugin class mock.
Plugin pluginMock = Mockito.mock(Plugin.class);
Mockito.when(pluginMock.getBundle().getSymbolicName()).thenReturn("com.app.test");
This pluginMock is injected to PluginLoggerFactory class Constructor and inside constructor, it call pluginMock.getBundle().getSymbolicName() method.
but I'm facing a NullPointerExcpetion because pluginMock is null and I can't call getBundle() on a Null value.
Please suggest how to mock Plugin class instance.
PS: - I also tried to call method chaining Mockito.RETURNS_DEEP_STUBS

Junit: Calling private method without instance

There are no Junits are written in application. But in other module we have started writing Junits using Jmockit. So any core java or Jmockit solution will be highly appreciated.
I have application initialization class, which is singleton, it initialize application by calling database[data load], property file etc. Its object creation is complex.
I am writing new method to initialize class [method name addMoreInitilization].
I want to write Junit for above scenario. Is there way to write Junit for addMoreInitilization method without calling complex object creation logic because it have lot of dependencies and mocking them all is not feasible and also do not want to make method static ?
Pseudo java code for ApplicationStartUp class below
Class ApplicationStartUp {
private ApplicationStartUp applicationStartUp;
// assume single ton implemented
private ApplicationStartUp () {
// complex object creation
addMoreInitilization();
}
public static getInstance() {
}
// new method
private addMoreInitilization() {
// my logic needs to be tested using junit
}
}
You can extend the "ApplicationStartUp" in your tests. Then override the unwanted methods. This way, you can create an Object of the subclass and call the method "addMoreInitialization" of the super class.

Configure Spring Boot Test to use separate sets of beans

I want for my Spring Boot test to use new instances for beans for each test run. Which part of the test configuration can I set it?
You may wanna take a look at the #DirtiesContext annotation. Once state of the bean will be modified, new context will be provided:
There are few options to run with, namely:
Before current test class: when declared at the class level with class mode set to BEFORE_CLASS
Before each test method in current test class: when declared at the class level with class mode set to BEFORE_EACH_TEST_METHOD
Before current test method: when declared at the method level with method mode set to BEFORE_METHOD
After current test method: when declared at the method level with method mode set to AFTER_METHOD
After each test method in current test class: when declared at the class level with class mode set to AFTER_EACH_TEST_METHOD
After current test class: when declared at the class level with class mode set to AFTER_CLASS
for further reading, please take a look at:
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/annotation/DirtiesContext.html
You can use #DirttiesContext like pezetem wrote.
But if you must have fresh instances of beans for test try refactor your tests and maybe code because you probably will have problems with working app.

Mocking a static method with a generic parameter

I have the following setup :
Class to test : SeriesOffset which extends BaseDisplayOption
Test Class : SeriesOffsetTest
When creating an object of the SeriesOffset class to test it, the constructor of the same makes a super call which then makes the following method call :
logger = LoggingService.getLog(this.getClass());
where LoggingService is an abstract class and getLog(Class<?> clazz) is a static method with a generic class parameter. This very method call needs to be mocked. I created a mock implementation for the same with a class called ILogImpl and this is how I am trying to test it:
ILogImpl a = new ILogImpl();
PowerMockito.mockStatic(LoggingService.class);
PowerMockito.when(LoggingService.getLog( SeriesOffset.class)).thenReturn(a);
But this method doesnt seem to work and it calls the real implementation instead of the mock one that I need it to call.
The error trace is the following : error trace
According to the error trace, I don't see where your real implementation of getLog is being called—but the real class initializer ("clinit") is being called as part of mock creation, because you're at least referring to the actual class and its static fields and static {} blocks are being loaded as usual.
Look to line 41 of LoggingService.java, and if the problem isn't obvious there, edit your answer so we can see it and diagnose further.

Injecting spring service in external groovy class created at runtime

I have an Spring-powered app and want to integrate groovy. Specifically, I have one abstract java class with a set of abstract method definitions and one repository inyected with autowired.
This class must be implemented by several final groovy external classes (one for each client).
At this moment, I am calling the Groovy class in java this way:
final Class parsedClass = groovyClassLoader.parseClass(groovyFile);
final GroovyObject groovyObject = (GroovyObject) parsedClass.newInstance();
final Object response = groovyObject.invokeMethod(methodName, methodParameters);
The problem is that I need to autowired the repository variable in each Groovy external class but currently are null.
How can I notify the Groovy class to get the inyected repository variable when I create it at runtime?
Thanks!
Edit
Y have solved it using the setProperty method from groovyObjectObject this way:
groovyObject.setProperty("myRepository", myRepositoryImpl);
The instance here is not created by spring, hence I don't think spring can automagically set the instance of repository in the groovyObject that you have.
However, if you can can autowire the repository into the class thats generating the groovyObject then you can manually inject the repo in the newInstance call.
parsedClass.newInstance(repository:your_autowired_repo_ref)
I have solved it using the setProperty method from groovyObjectObject this way:
groovyObject.setProperty("myRepository", myRepositoryImpl);

Categories

Resources