Dropwizard Integrated Testing with TestResource - java

Does anyone knows how to add a test Resource (i.e. one that is only for testing purposes and not added in run() method of the app)?
Here is an example:
public class MyTest {
#ClassRule
public static final DropwizardAppRule<TestConfiguration> RULE =
new DropwizardAppRule<TestConfiguration>(MyApp.class, "my-app-config.yaml");
#BeforeClass
public static void setUpBeforeClass() throws Exception
{
MyTest.RULE.getEnvironment().jersey().register(new JustForTestingResource());
}
#Test
public final void testTestResource()
{
Client client = new Client();
ClientResponse response = client.resource(
String.format("http://localhost:%d/rest/v1/test", RULE.getLocalPort()))
.get(ClientResponse.class);
assertThat(response.getStatus(), is(200));
}
}
and
public class JustForTestingRessource {
#GET
#Path("test")
#Produces(MediaType.APPLICATION_JSON)
public Response getInTestResource()
{
return Response.status(Status.OK).type(MediaType.TEXT_PLAIN).entity("get #Path(\"test\") is ok").build();
}
}
My problem is that the added resource is not added and I get resource not found 404 error response. It seems that I am registering the new resource after resource publishing and there is no refresh inside Dropwizard after start.
I dont want to extend my Application class and I dont want to insert test code into my real application code. Does anyone knows how to register the test resource without registering it in run() method of the Application?
This works, but a new class is needed:
public class TestService extends MyService{
#Override
public void run(
TestConfigurationconfiguration,
Environment environment) throws ClassNotFoundException
{
environment.jersey().register(new JustForTestingRessource());
super.run(configuration,environment);
}
}
Call in JUnit as already known:
#ClassRule
public static DropwizardAppRule<TestConfiguration> RULE =
new DropwizardAppRule<TestConfiguration>(TestService.class, "my-app-config.yaml");

Edit: Removing previous answer because it didn't solve your problem the way you wanted to do it.
I dug into the environment startup code and realized the reason why registering a controller didn't make it available is because jetty had already been started. If you stop jetty, register your controller and start jetty back up again, your resource will be available and you can use it in your test.
#BeforeClass
public static void setUpBeforeClass() throws Exception
{
MyTest.RULE.environment.applicationContext.stop()
MyTest.RULE.environment.jersey().register(new JustForTestingResource())
MyTest.RULE.environment.applicationContext.start()
}

You can test the Resource itself in a Jersey Container without starting a full dw-instance.
Check the "Testing Resources" section.
import static org.fest.assertions.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
public class PersonResourceTest {
private static final PeopleStore dao = mock(PeopleStore.class);
#ClassRule
public static final ResourceTestRule resources = ResourceTestRule.builder()
.addResource(new PersonResource(dao))
.build();
private final Person person = new Person("blah", "blah#example.com");
#Before
public void setup() {
when(dao.fetchPerson(eq("blah"))).thenReturn(person);
// we have to reset the mock after each test because of the
// #ClassRule, or use a #Rule as mentioned below.
reset(dao);
}
#Test
public void testGetPerson() {
assertThat(resources.client().resource("/person/blah").get(Person.class))
.isEqualTo(person);
verify(dao).fetchPerson("blah");
}
}

I had the similar issue with the #ClassRule, maybe it can help to somebody..
In my test (Groovy) the invocation of RULE.getApplication() or getEnvironment() from #BeforeClass method returned null:
def setupSpec() {
RULE.application.run()
}
shown
java.lang.NullPointerException: Cannot invoke method run() on null object
I.e. RULE.testSupport had both null application and environment.
I found out that the call to
RULE.testSupport.before()
just before run() solves the error:
def setupSpec() {
RULE.testSupport.before()
RULE.application.run()
}
And then #AfterClass method:
def cleanupSpec() {
RULE.testSupport.after()
}
Or just use #Rule instead of #ClassRule and call
def setup() {
RULE.application.run()
}
inside of #Before method instead of #BeforeClass.
Though It seems strange, maybe there is some other better solution exists..

public class TestMain extends Main{
public static void main(String ... args) throws Exception {
new TestMain().run(args);
}
#Override
public void initialize(Bootstrap<AppConfiguration> bootstrap) {
super.initialize(bootstrap);
bootstrap.addBundle(
new MigrationsBundle<AppConfiguration>() {
#Override
public DataSourceFactory getDataSourceFactory(
AppConfiguration configuration) {
return configuration.getDataSourceFactory();
}
});
}
}

Related

How to launch application after each test Junit5 TestFX

I'm writing an application test with Junit5 and TestFX.
My intention is that the main test class relaunches the application after each test. As far as I know, I shall use the annotation #BeforeEach, and it didn't work for me.
Here is my test class:
#FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class MainTest extends ApplicationTest implements FxRobotInterface {
Logger loggerGuiTesting = LoggerManager.getInstance().getLogger(LoggerType.GUI_TESTING);
#BeforeEach
#Override
public void start(final Stage stage) {
StartMain.getInstance();
this.loggerGuiTesting.log(Level.INFO, "Application starts!");
}
#AfterAll
public void endApplication() {
new ExitGuiTest().run(); // That's my internal test framework
}
#Test
public void atestIfOpeningScreenIsThere() {
verifyThat("#imageViewSplashScreenLogo", NodeMatchers.isNotNull());
verifyThat("#progressBarSplashScreen", NodeMatchers.isNotNull());
verifyThat("#labelSplashScreenVersion", NodeMatchers.isNotNull());
verifyThat("#labelSplashScreenDate", NodeMatchers.isNotNull());
this.loggerGuiTesting.log(Level.INFO, "testIfOpeningScreenIsThere, succeeded!");
}
#Test
public void btestIfRadioButtonOneExist() {
assertThat("#sourcesOneRadioButton", is("#sourcesOneRadioButton"));
this.loggerGuiTesting.log(Level.INFO, "testIfRadioButtonOneExist, succeeded!");
}
#Test
public cnextTest() {
new StartAnotherGuiTest().run();
this.loggerGuiTesting.log(Level.INFO, "anotherTest, succeeded!");
}
}
The question is: how can I relaunch the application after each test?
It is difficult to answer without taking a look at the StartMain class. It looks like you are using a singleton pattern there. If thats the case I would create a new method in StartMain that sets the singleton instance to null so when getInstance is called again, it has to be re-created:
#After //This should be executed after each test
public void destroyApp()
{
StartMain.getInstance().destroy();
}

Injecting mock before Spring's post-construct phase

Basically, the question is in the title.
I faced a problem that in post-construct phase my bean (that is autowired in the bean that is going through post-construct phase right now) is already mocked, but all the behavior described by Mockito.when() doesn't work, all the calls return null.
While searching I found this solution.
But is it possible to make it work without using any 3rd party libraries?
Test class:
#RunWith(SpringRunner.class)
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
#ContextConfiguration(classes = TestApplicationConfiguration.class)
public class ServiceTest {
#Autowired
#Qualifier("test")
private PCLPortType pclPortType;
#MockBean
private ClearingHelper сlearingHelper;
#MockBean
private OrganizationCacheRepository organizationCacheRepository;
#Before
public void setup() throws Exception{
OperationResultWithOrganizationSystemIdMappingList res = new OperationResultWithOrganizationSystemIdMappingList();
when(clearingHelper.getOrgIdSystemIdMapping(any(Keycloak.class))).thenReturn(res);
}
#Test
public void test() throws Exception{
pclPortType.call("123");
}
}
Test config:
#TestConfiguration
public class TestApplicationConfiguration {
#Bean(name = "test")
public PCLPortType pclPortTypeForTest() throws JAXBException {
...
}
#Bean
public Keycloak keycloak() {
return Mockito.mock(Keycloak.class);
}
}
Component where I want to get mocked beans:
#Component
public class OrganizationCacheJob {
private static final Logger logger =
LogManager.getLogger(OrganizationCacheJob.class);
private final ObjectFactory<Keycloak> factory;
private final ClearingHelper clearingHelper;
private final OrganizationCacheRepository organizationCacheRepository;
#Autowired
public OrganizationCacheJob(ObjectFactory<Keycloak> factory,
ClearingHelper clearingHelper,
OrganizationCacheRepository organizationCacheRepository) {
this.factory = factory;
this.clearingHelper = ClearingHelper;
this.organizationCacheRepository = organizationCacheRepository;
}
#PostConstruct
public void updateCacheRepository() {
doUpdateCacheRepository();
}
#Scheduled(cron = "${organization.cache.schedule}")
public void start() {
logger.info("Starting update organization cache.");
doUpdateCacheRepository();
logger.info("Job finished.");
}
private void doUpdateCacheRepository() {
try {
Keycloak keycloak = factory.getObject();
OperationResultWithOrganizationSystemIdMappingList orgIdSystemIdMapping = clearingHelper.getOrgIdSystemIdMapping(keycloak);
if (orgIdSystemIdMapping != null) {
orgIdSystemIdMapping.getContent().forEach(o -> organizationCacheRepository.saveOrgIdsSystemsIdsMappings(o.getOrgId(), o.getId()));
logger.debug("Was saved {} orgIds", orgIdSystemIdMapping.getContent().size());
}
} catch (Exception e) {
logger.error("Error fetching whole mapping for org and systems ids. Exception: {}", e);
}
}
}
So, in post-construct phase of OrganizationCacheJob I want to get res when calling clearingHelper, but instead I get null.
ClearingHelper is a regular Spring bean marked as a #Component with public methods.
Ahh ok I just realized - when you start your test case, whole env is up and running first, then you advance to testing phase. So, translating to your case - first you got injection and post-constructs called, then #Before method is done, thus the result.
So as you can see, code says more than all the words you could put in your original post.
If it is possible for you, use spies insteed of mocks. If it is not possible to construct that, you will have to redesign your tests to not rely on post construct.
In this case, since you want the same post-construct behavior for every test case, provide own factory method for given mock (like you did with keycloak) and move when-doReturn there. It will be guaranteed that it will happen before post construct.

Spring Boot: how to inject dependencies into a class called by a library?

I'm using Kinesis Client Library (KCL) and Spring boot. To use KCL, I have to implement a class (I named it RecordProcessor) for interface IRecordProcessor. And KCL will call this class and process records from kinesis. But when I tried to use dependency injection, I found it was not succeeded.
Here's the snippet for RecordProcessor:
#Component
public class RecordProcessor implements IRecordProcessor {
#Autowired
private SingleRecordProcessor singleRecordProcessor;
#Override
public void initialize(String shardId) {
...
}
#Override
public void processRecords(List<Record> records, IRecordProcessorCheckpointer checkpointer) {
...
}
}
I use Class SingleRecordProcessor to process single each record from kinesis. And this is my SingleRecordProcessor class snippet:
#Component
public class SingleRecordProcessor {
private Parser parser;
private Map<String, Table> tables;
public SingleRecordProcessor() {
}
#Autowired
private void setParser(Parser parser) {
this.parser = parser;
}
#Autowired
private void setTables(Map<String, Table> tables) {
this.tables = tables;
}
public void process(String record) {
...
}
}
I want to let spring framework automatically inject the SingleRecordProcessor instance into the class and use it. But I found that the field singleRecordProcessor is null.
Any idea why the dependency injection is failed? Or is it impossible to inject dependencies into a class which is called by other framework (in this case it's KCL)? Any suggestions will be appreciated! Really need some help please!!
[UPDATE]:
Sorry for not expressing the error clearly. The error was NullPointerException. I tried to inject singleRecordProcessor and call method process() on it. I think the injection was not successful so the instance singleRecordProcessor is null and there comes the NullPointerException.
More information is as follows:
I have a major class called Application
#SpringBootApplication
public class Application{
public static void main(String[] args) {
SpringApplication application = new SpringApplication(Application.class);
application.addListeners(new ApplicationPidFileWriter("./app.pid"));
ConfigurableApplicationContext ctx = application.run(args);
}
}
And I have the MainProcessor class which will call KCL.
#Service
public final class MainProcessor {
#EventListener(ApplicationReadyEvent.class)
public static void startConsumer() throws Exception {
init();
IRecordProcessorFactory recordProcessorFactory = new RecordProcessorFactory();
Worker worker = new Worker(recordProcessorFactory, kinesisClientLibConfiguration);
...
worker.run(); // this line will call KCL library and eventually call ProcessorRecord class.
}
}
[UPDATE2]
RecordProcessorFactory only has one method like this
#Component
public class RecordProcessorFactory implements IRecordProcessorFactory {
#Autowired
RecordProcessor recordProcessor;
#Override
public IRecordProcessor createProcessor() {
return recordProcessor;
}
}
It creates a new RecordProcessor instance for KCL to use it.
You should autowire an instance of this into your MainProcessor:
#Component
public class RecordProcessorFactory {
#Lookup IRecordProcessor createProcessor() { return null; }
}
Spring will instantiate a RecordProcessorFactory for you, and replace the implementation of createProcessor() in it with one that will return a new IRecordProcessor each time it's called. Both the factory and the processors will be Spring beans - which is what you want.

Mock System.class per method in Junit

I am facing issue to get the System.getProperty("name") value per method vise after mocking System.class
Example:
#Test
public void Test1()throws Exception {
PowerMockito.mockStatic(System.class);
// Configuration
when(System.getProperty("os.name")).thenReturn("Win");
}
#Test
public void Test2() throws Exception {
PowerMockito.mockStatic(System.class);
when(System.getProperty("os.name")).thenReturn("Linux");
}
We have two tests method as mentioned above when we are running both the method independently then we are getting correct value of System.getProperty("os.name"). But when we are running class (will execute both method in class) then we are getting first method's System.getProperty("os.name") value in second method.
Please suggest.
Thank in advance.
I would use an extra external library, similar to System Rules.
You can then achieve your goal by doing the following:
public class SystemRulesTest {
#Rule
public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties();
#Test
public void test1() {
System.setProperty("os.name", "Win");
}
#Test
public void test2() {
System.setProperty("os.name", "Linux");
}
}
Note: System Rules requires Junit 4.9 or above to work.
If you want to change the system property, why not just System.setProperty("os.name", "")? This works:
public class SystemPropertyTest {
#Test
public void testWin() {
System.setProperty("os.name", "Win");
Assert.assertEquals("Win", System.getProperty("os.name"));
}
#Test
public void testLinux() {
System.setProperty("os.name", "Linux");
Assert.assertEquals("Linux", System.getProperty("os.name"));
}
}

How to execute a Java main class using #Configuration in a gradle project right after the beans are created

I have a config class that instantiates all the beans. I have a main class
TestUpdator() with an execute().
I plan to run this main class when I do a "gradle tomcatRunWar" to run the war which will generate the beans and run the execute() in the main class.
Example.
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = "<packageName>", excludeFilters = { #Filter(type = FilterType.ANNOTATION, value = Service.class) })
#EnableSolrRepositories(basePackages = "<solrPackageName>", multicoreSupport = true)
public class JobConfig {
private static final Logger logger = LoggerFactory.getLogger(JobConfig.class);
#Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
....
}
#Bean
public SpringContextAware springContextAware() throws Exception {
.....
}
.....
//MAIN CLASS BEAN TO BE EXECUTED
#Bean(destroyMethod="shutdown")
public TestResultUpdator testResultUpdator() throws Exception {
TestResultUpdator resultUpdator = new TestResultUpdator();
/**
Bunch of dependencies for this bean
**/
resultUpdator.execute(); //call to execute()
return resultUpdator;
}
The TestUpdator class looks like this
public class TestResultUpdator {
//Variables
public static void main(String args[]) throws Exception {
//NOT SURE WHAT TO ADD HERE SINCE THE execute() will be called from the class above. But this is what I have right now commented out.
// ApplicationContext appContext = new AnnotationConfigApplicationContext(JobConfig.class);
// TestResultUpdator updator = appContext.getBean(TestResultUpdator.class);
// updator.execute();
}
public void execute() throws Exception {
logger.info("INSIDE EXECUTE FOR TEST UPDATOR!!!!");
while(true) {
//CODE TO BE EXECUTED
}
}
}
This class will execute in the background and would be executing code when the war is run.
Is there a way to do it the way I have it or is there an alternate way?
I have googled around and am not able to find good examples of this.
Basically what I am trying to do it two things,
1) when I do gradle tomcatRunWar, it will start tomcat and deploy the war
2) when it does that it should run the main class (TestUpdator) in the background by calling the execute()
Well, unless you're developing is a Spring Boot project, term main class is meaningless here.
If you're trying to execute a piece of code in TestResultUpdator class during context initialization, you can follow these steps:
1 Organize TestResultUpdator class as follows:
#Bean
public class TestResultUpdator {
#PostConstruct
public void execute() {
//CODE TO BE EXECUTED
}
}
2 Get this bean somewhere in your code:
#Autowire
private TestResultUpdator bean;

Categories