This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Spring and the fly likes How to create the Object for @Autowired field using Junit test? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How to create the Object for @Autowired field using Junit test?" Watch "How to create the Object for @Autowired field using Junit test?" New topic
Author

How to create the Object for @Autowired field using Junit test?

Laxmi Prasanna Bn
Greenhorn

Joined: Feb 13, 2012
Posts: 17
Iname (Interface)
-------
Interface IName{
String getName();
}


FullName
-------
Class FullName implements IName
{
getName(){
String name="hello";
}
}

class ADelegate
--------
Class ADelegate{

@Autowired
IName name;

public Request createRequest(){
FullName.getName(); /* Here how to create the object for FullName class
}

Class TestADelegate
-------------
@Autowired
ADelegate aDelegate;

public void testCreateRequest(){

aDelegate.createRequest();

}

From above code while i'm doing JUNIT test i couldn't able to create the Object for FullName class.
so how do we configure beans.xml file for creating the Object for FullName class (Using JUNIT Test)??

Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1650
    
    7

Please UseCodeTags.



You typically should not pull in mvc beans as test cases do not run in a servlet container. If you really want to do this and you are using the newest version of Spring you can use the test-mvc package for that. There is more on that in the reference docs.


[How To Ask Questions][Read before you PM me]
Laxmi Prasanna Bn
Greenhorn

Joined: Feb 13, 2012
Posts: 17
Thanks for your's valuable replay..

i'm going to be use MVC test, so here my concern is how it would be the "applciationContext.xml" file.

1) i have numbers of COntrollers and Delegates so , do i need to use this tag in XML file?

<context:component-scan base-package="com.td.term"></context:component-scan>
&&
interface and their impliment classes injection (IName -FullName)

(Correct me if any thing do we must should add some other code)
subash Kumar Tiwari
Greenhorn

Joined: Aug 12, 2008
Posts: 1


Hi All,
I am going to give explanation of above query as per my understanding.

Actually what I understand that, you are trying to inject a class in test class. Because both are handled by different container so your object will not be
wired automatically. you have below option's to test it.

And I am assuming that the classes you are trying to inject is POJO and application is pure java application not web application.

Below is step for java application not running through any server:

1. configure the POJO classes in context file . And load the context file through implementation classes like
ClasspathXmlApplicationContext Or FileSystemXmlApplicationContext etc.

then get the bean thriugh that and use.

2. If this is Web appplication then you have to build your test classes in support of test package.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1650
    
    7

Please UseCodeTags <- click when posting code snippets.

Yes. The SpringJUnit4ClassRunner class will take care of making the test case spring aware. The context configuration should be your spring config classes or xml files (depending if you are doing your configuration in xml or java), this part will take care of initializing your contexts for you. The class involved in your test case (typically this should be only 1 if you are writing good unit tests) may have autowired dependencies. In this case they must be defined in the xml or java config file you provide, so that it can be injected by Spring. Best practice says you create a smaller config file for that test case. This way you are not creating a bunch of beans for stuff that is not under test. You can also use a framework like Mockito to inject mocks rather than real objects. In this way you can prevent other classes from becoming involved in the code under test.

While you could could use component scan in your test config you can also just create the beans you need.




(this bean would be defined in the application-context.xml that is being fed to the @ContextConfiguration)
Laxmi Prasanna Bn
Greenhorn

Joined: Feb 13, 2012
Posts: 17
Thanks for your valuable replays..

I tried by using mockMVC,mockSession by using SPRING 3.2.1,but after code implementaion and started JUNIT RUN,
got the Jar version's mismatch on my project

SO Please provide one small sample example for Spring 3.0.5 MVC, JUNIT 4 Example for passing session from
TestController--->Controller---->Delegate.

In TestController mockSession is created.
I need to use this mock session in controller and Delegate.

Thanks..
Laxmi Prasanna Bn
Greenhorn

Joined: Feb 13, 2012
Posts: 17
Finally got a solution,

By using
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
(
"classpath:applicationContext.xml"
}

and

<bean id="sessionBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="com.td.term.session.sessionBean" />
<property name="target">
<bean class=" com.td.term.controllers.test.MockSessionImpl">
</bean>
</property>
</bean>


Here from MockSessionImpl class i'm added hardocded session.

and from applicationContext.xml by injecting the mock session , issues is fixed.
 
GeeCON Prague 2014
 
subject: How to create the Object for @Autowired field using Junit test?