Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to inject a mocked dependency into the tested instance?
How to inject a mocked dependency into the tested instance? [message #1701182] Thu, 09 July 2015 16:51 Go to next message
kon f is currently offline kon fFriend
Messages: 152
Registered: March 2012
Senior Member
Hey Smile

my StringGenerator class accesses the workspace being able to look up certain information from different files in the workspace. Now I'm writing an isolated unit test that should work without the eclipse workspace. Unfortunately I'm not able to inject the mocked dependency.

How do I inject the mocked instance of ResourceLookUp into the StringGenerator instance?

The class to be tested:
class StringGenerator {

	@Inject extension ResourceLookUp resourceLookUp

	def dispatch initStep(TestRequired o) '''
		Test Case «o.name.testNameLookUp» (Test Case ID: «o.name») has been successfully performed.'''
}


The Test:
import static org.mockito.Mockito.*

@RunWith(JUnitParamsRunner)
class StringGeneratorInitialisationTest {

	@Inject extension ValidationTestHelper validationTestHelper = new ValidationTestHelper
	@Inject extension StringGenerator stringGenerator = new StringGenerator
	@Inject extension ParseHelper<Model> parseHelper = new ParseHelper

	@Before
	def void before() {
		var i = new TestStandaloneSetup().createInjectorAndDoEMFRegistration
		i.injectMembers(parseHelper)

		val resourceLookUp = mock(ResourceLookUp)
		when(resourceLookUp.testNameLookUp("TEST XYZ")).thenReturn("TEST NAME AS STRING")
		// how do I get this mocked ResourceLookUp instances injected into StringGenerator instance?


		i.injectMembers(stringGenerator)
		i.injectMembers(validationTestHelper)
	}

	 // here comes my test ....
}


I was playing around with the debugger but could not find any solution Sad

Thank you!

Kon
Re: How to inject a mocked dependency into the tested instance? [message #1701196 is a reply to message #1701182] Thu, 09 July 2015 19:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
simply use a StandaloneSetup that uses a Module that has an instance binding for the thing you are looking for

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to inject a mocked dependency into the tested instance? [message #1701236 is a reply to message #1701196] Fri, 10 July 2015 07:48 Go to previous messageGo to next message
kon f is currently offline kon fFriend
Messages: 152
Registered: March 2012
Senior Member
Hey Christian,

thank you for your reply Smile Do you have an example how to achieve this? I tried the following but could not find a Guice-Factory to wrap the actual instance with the Binding type.

val resourceLookUp = mock(ResourceLookUp)
when(resourceLookUp.testNameLookUp("TEST XYZ")).thenReturn("TEST NAME AS STRING")
Binding<ResourceLookUp> instance = // WRAP resourceLookUp instance with Binding interface
i.bindings.put(TypeLiteral.get(ResourceLookUp),  instance)


I also thought of getting the modul (couldn't find the it in IjectorImpl) and use:

bind(ResourceLookUp).toInstance(resourceLookUp)


Thank you! Smile

Kon
Re: How to inject a mocked dependency into the tested instance? [message #1701237 is a reply to message #1701236] Fri, 10 July 2015 07:52 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
no i have not. you have to read, understand and modify the code of the standalonesetup.createinjector yourself

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to inject a mocked dependency into the tested instance? [message #1701247 is a reply to message #1701237] Fri, 10 July 2015 08:45 Go to previous messageGo to next message
kon f is currently offline kon fFriend
Messages: 152
Registered: March 2012
Senior Member
Hey Christian,

thank you again! I actually thought I that I could simply exchange the map value from the bindings map. But I'm not sure if this is an intended. So I did what you suggested:

@Before
	def void before() {
		val i = new TestStandaloneSetup() {
			override createInjector() {
				Guice.createInjector(new TestRuntimeModule() {
					override configure(Binder binder) {
						super.configure(binder)

						val resourceLookUp = mock(ResourceLookUp)
						when(resourceLookUp.testNameLookUp("TEST XYZ")).thenReturn("TEST NAME AS STRING")
						binder.bind(ResourceLookUp).toInstance(resourceLookUp)
					}
				})
			}
		}.createInjectorAndDoEMFRegistration

		i.injectMembers(parseHelper)
		i.injectMembers(stringGenerator)
		i.injectMembers(validationTestHelper)
	}


Best regards,

Kon
Re: How to inject a mocked dependency into the tested instance? [message #1702578 is a reply to message #1701247] Thu, 23 July 2015 11:47 Go to previous messageGo to next message
kon f is currently offline kon fFriend
Messages: 152
Registered: March 2012
Senior Member
Hey,

noticed that the injection does not work completely as expected. The StringGenerator class gets the mocked ResourceLookUp instance.

class StringGenerator {

	@Inject extension ResourceLookUp resourceLookUp

	def dispatch initStep(TestRequired o) '''
		Test Case «o.name.testNameLookUp» (Test Case ID: «o.name») has been successfully performed.'''
}


but if the test-case contains an error assertion like model.assertNoErrors my TestValidator gets called. Unfortunately the ResourceLookUp instance there is not the mocked one Sad


class TestValidator extends AbstractTestValidator {

	@Inject extension ResourceLookUp resourceLookUp 

 	// ...
}


It somehow ignores my guice setup:

val mockedResourceLookUp = mock(ResourceLookUp)
when(mockedResourceLookUp.testNameLookUp("TEST XYZ")).thenReturn("TEST NAME AS STRING")
bind(ResourceLookUp).toInstance(mockedResourceLookUp)
 


and uses the "real" class for injection. Is there anything spezial about the dependency injection for the validator class?

Thank you.

Kon
Re: How to inject a mocked dependency into the tested instance? [message #1702581 is a reply to message #1702578] Thu, 23 July 2015 11:59 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
the validators are created early

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to inject a mocked dependency into the tested instance? [message #1702884 is a reply to message #1702581] Mon, 27 July 2015 08:35 Go to previous message
kon f is currently offline kon fFriend
Messages: 152
Registered: March 2012
Senior Member
Hey Christian,

if I let my test class derive from AbstractXtextTests, it works as expected Smile

import static org.mockito.Mockito.*

@RunWith(JUnitParamsRunner)
class StringGeneratorInitialisationTest extends AbstractXtextTests {

}


Thank you!
Previous Topic:How to align text elements of more lines
Next Topic:global scope and importedURI
Goto Forum:
  


Current Time: Fri Apr 26 03:15:26 GMT 2024

Powered by FUDForum. Page generated in 0.03426 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top