Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9296671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:53:01+00:00 2026-06-18T21:53:01+00:00

I am writing junit test case for spring 3 restful services. When I am

  • 0

I am writing junit test case for spring 3 restful services. When I am trying to execute it as junit, i am getting JdbcTemplate as null. I am not sure where I did the mistake. Please help me to get out of this…

LoginServiceImpl.java file,

private NamedParameterJdbcTemplate jdbcTemplate;

@Autowired
public void setDataSource(DataSource dataSource) {
    jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
    System.out.println("--------------"+jdbcTemplate.toString());
}

private static Map<String, AuthToken> tokenHash = new ConcurrentHashMap<String, AuthToken>();

private static String authTokenDetailsSql = "select * from authtoken where token = :token";

@Override
@RequestMapping(value = "/register", method = RequestMethod.POST)
@ResponseBody
public ServiceBean newAccount(@RequestBody Registration registration) {
    String newAccountSql = "INSERT INTO account (email,password,name) VALUES (:email,:password,:name)";
    ServiceDataBean<AuthToken> retBean = new ServiceDataBean<AuthToken>();
    try {
        System.out.println("register service calling.....");
        MapSqlParameterSource namedParameters = new MapSqlParameterSource();
        namedParameters.addValue("email", registration.getEmail());
        messageDigest = MessageDigest.getInstance("MD5");
        byte[] md5 = new byte[64];
        messageDigest.update(registration.getPassword().getBytes("iso-8859-1"), 0, registration.getPassword().length());
        md5 = messageDigest.digest();
        namedParameters.addValue("password", convertedToHex(md5));
        namedParameters.addValue("name", registration.getName());
        GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();

        // TODO what to do with the updInt also check it's not -1
        int updInt = jdbcTemplate.update(newAccountSql, namedParameters, generatedKeyHolder);
        long accountId = (Long) generatedKeyHolder.getKeys().get("GENERATED_KEY");
        registration.getDevice().setOwner(registration.getId());
        fotoframz.register(registration.getDevice());
        Login login = new Login();
        login.setEmail(registration.getEmail());
        login.setPassword(registration.getPassword());
        login.setDevice(registration.getDevice());
        retBean = (ServiceDataBean<AuthToken>) this.login(login);
        System.out.println("form register");
    } catch (Throwable e) {
        retBean.setStatusCode("001");
        e.printStackTrace();
    }
    return retBean;
}

I am getting jdbctemplate=null at int updInt = jdbcTemplate.update(newAccountSql, namedParameters, generatedKeyHolder);

my applicationContext-test.xml file is in src/test/resources folder..applicationContext-test.xml file

<context:component-scan base-package="net.mss.ff.services" />       
<context:property-placeholder location="classpath:/app.properties" />

<!-- <task:annotation-driven /> -->
<context:annotation-config />
<!-- <import resource="apicontroller_v1-servlet.xml"/>
<import resource="applicationContext.xml"/> -->

<bean id="photoService" class="net.mss.ff.services.core.api.impl.PhotoServiceImpl">
    <property name="rootStorageFolder" value="${storage.root}" />
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" />

LoginServiceImplTest

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/applicationContext-test.xml"})
public class LoginServiceImplTest {

LoginServiceImpl loginServiceObj = new LoginServiceImpl();
Device deviceMock;

@Autowired
private Fotoframz fotoframz;

/*private NamedParameterJdbcTemplate jdbcTemplate;

@Autowired
public void setDataSource(DataSource dataSource) {
    jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}*/

@Before
public void setUp() throws Exception {
    //loginServiceObj = new LoginServiceImpl();
}

@After
public void tearDown() throws Exception{

}

/**
 * Test method for {@link net.mss.ff.services.core.api.impl.LoginServiceImpl#newAccount(net.mss.ff.services.core.beans.Registration)}.
 */
@Test
public void testNewAccount() {
    Registration mockRegObj = new Registration();
    deviceMock = new Device();
    deviceMock.setActive(false);
    deviceMock.setHeight(45);
    //deviceMock.setId(4568);
    deviceMock.setName("Android");
    deviceMock.setOwner(1111);
    deviceMock.setPlatform("Windows NT");
    deviceMock.setUuid("522601");
    deviceMock.setVersion("1.0");
    deviceMock.setWidth(76);
    mockRegObj.setEmail("bbb@gmail.com");
    /*mockRegObj.setId(399);*/
    mockRegObj.setName("bbb");
    mockRegObj.setPassword("BBB");
    mockRegObj.setDevice(deviceMock);
    loginServiceObj.newAccount(mockRegObj);

    //assertEquals("New Account Creation", "", "");
}

}
anything needs to modify in test class, please let me know..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T21:53:02+00:00Added an answer on June 18, 2026 at 9:53 pm

    In your test the LoginServiceImpl loginServiceObj = new LoginServiceImpl();
    is not instantiated by spring, thus no annaotions will be applied. You need to autowire it, or inject it some other way. Spring 3.2 makes this kinda thing super easy and nice to use.

    The rest of my answer is still good adivce :
    You have not declared or instantiated the jdbctemplate in your java code. And you have not defined it in your xml file.

    You need this

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource">
                <ref bean="dateSource"/>
            </property>
        </bean>
    

    and then this as instance variable (assuming your using annoations)

       @Resource(name = "jdbcTemplate")
        private JdbcTemplate jdbcTemplate;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Choose order to execute JUnit tests I am writing a JUnit test.
I am writing a JUnit test case for my Android app. The test class
I am writing a Junit test framework to test web services. There is a
I'm using Spring 3.1.0.M2. I'm writing some JUnit 4 tests to test some database
I'm writing a JUnit test for some code that produces an Excel file (which
I'm writing a jUnit test for a constructor that parses a String and then
I am writing a junit test in order to (surprisingly) test a part of
I've been trying to understand how to start writing and running JUnit tests. When
I'm writing a JUnit test using JUnitPerf. Here, I want to generate some entries
I'm writing a JUnit test method for a Clock class to ensure that the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.