inject into unit test (only XML)
Tag : java , By : DotNetWise
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further What methods in the DAO do you want to unit test? Are you not actually trying to test the DB connectivity? Otherwise I agree with @duffymo. final ClassPathXmlApplicationContext beanFactory =
new ClassPathXmlApplicationContext( "applicationContext.xml" );
AutowireCapableBeanFactory factory = beanFactory.getAutowireCapableBeanFactory();
factory.autowireBeanProperties( this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE , true );
ProductDao dao = context.getBean( "productDao" );
|
Couldn't Inject Service class in grails unit test- Test Fails
Date : March 29 2020, 07:55 AM
this one helps. Assuming you are using a version before 2 (and JUnit instead of Spock), you need to manually add the service: class QuoteServiceTests extends GrailsUnitTestCase {
def quoteService
void setUp() {
quoteService = new QuoteService()
}
void testStaticQuote() {
def staticQuote = quoteService.getStaticQuote()
// ...
@TestFor(QuoteService) // Allows you to call the QuoteService via 'service'
class QuoteServiceTests {
void testStaticQuote() {
def staticQuote = service.getStaticQuote()
// ...
|
How to inject module in test for requirejs to unit test application?
Date : March 29 2020, 07:55 AM
Does that help This code pattern is not unit testable. I guess someone could come up with a way of tricking RequireJS to load different module definition and unload it at the end of the test but that would be too complicated and too error prone, and wouldn't solve the problem of bad coding practice. The pattern is not testable because inside of moduleB definition you are creating new instance of moduleA (plus loading of ModuleA is "hardcoded"). To make it testable you need to inject it. Consider something like: define('moduleB', function() {
var ModuleB = function(moduleA) {
this.__moduleA = moduleA;
this.doSomethingElse = function() {
if(this.__moduleA.doSomething()) {
// do something else and return
} else {
// do other things and return
}
}
}
return ModuleB
});
|
Jersey 2 inject dependencies into unit test
Date : March 29 2020, 07:55 AM
Any of those help You can use the main IoC container, and just explicitly inject the test class. Jersey uses HK2 as its DI framework, and its IoC container is the ServiceLocator, which has a method inject(anyObject) that can inject any objects with dependencies that are in its registry. For example you could do something like public class InjectionTest {
@Inject
private TestController controller;
@Before
public void setUp() {
final Binder b = new AbstractBinder() {
@Override
public void configure() {
bindAsContract(TestController.class);
}
};
final ServiceLocator locator = ServiceLocatorUtilities.bind(new TestBinder(), b);
locator.inject(this);
}
@Test
public void doTest() {
assertNotNull(controller);
String response = controller.get();
assertEquals("Hello Tests", response);
}
}
public abstract class AbstractControllerTest {
protected ServiceLocator locator;
private final Class<?> controllerClass;
protected AbstractControllerTest(Class<?> controllerClass) {
this.controllerClass = controllerClass;
}
@Before
public void setUp() {
final AbstractBinder binder = new AbstractBinder() {
@Override
public void configure() {
bindAsContract(controllerClass);
}
};
locator = ServiceLocatorUtilities.bind(new TestBinder(), binder);
locator.inject(this);
}
@After
public void tearDown() {
if (locator != null) {
locator.shutdown();
}
}
}
public class TestControllerTest extends AbstractControllerTest {
public TestControllerTest() {
super(TestController.class);
}
@Inject
private TestController controller;
@Test
public void doTest() {
assertNotNull(controller);
assertEquals("Hello Tests", controller.get());
}
}
|
How to unit test method where you cannot inject dependency
Date : March 29 2020, 07:55 AM
|