1

I was curious of there was a way for me to run a repository impl containing autowired candidates, with an application main() (for testing purposes).

i know the problem is that the repo class containing the main isn't being wired in the main() method, which is causing none of the autowired candidates to be wired but I'm having trouble finding the syntax to make this happen.

The sample code snippet is below, as well as comments in the section where I'm encountering the issue

@Repository
@Transactional
public class RepositoryImpl implements Repository {

    @Autowired
    JdbcTemplate jdbcTemplate;

    String query = "some query"

    @Override
    public boolean runQuery() {
        Object[] params = {param1, param2};
        boolean result = jdbcTemplate.queryForObject(query , params, Integer.class) == 1;
        return result;
    }

    //included just for testing purposes
    public static void main(String[] args) {
        /*this is where i would like to run runQuery() to see a sample output
        * something like this: 
        * repositoryImpl = new RepositoryImpl();
        * syso(repositoryImpl.runQuery());
        */
    }
} 

I run into a null pointer which is coming from jdbc template not being wired properly. Could someone direct me as to how to autowire a class with a self contained application main()?

Is this even possible?

Thanks for your help!

nashpatty
  • 23
  • 3
  • are you sure that your query return something i assume that you get a NullPointerException here `jdbcTemplate.queryForObject(query , params, Integer.class)` so please show us more details so we can help you – Youcef LAIDANI Nov 13 '17 at 18:40
  • beside you have to compare Integers using `.equals()` not `==` – Youcef LAIDANI Nov 13 '17 at 18:42
  • unfortunately the only thing the stacktrace shows me is this `java.lang.NullPointerException: null` and the problem line is indeed `jdbcTemplate.queryForObject(query , params, Integer.class)` – nashpatty Nov 13 '17 at 18:50
  • like i said show us your query please – Youcef LAIDANI Nov 13 '17 at 18:52
  • I'm really sorry but the query contains sensitive information, but I tested the query in sql dev to make sure i get the result i'm expecting and it works there. I've even printed out the string version of the query and copied that to sql developer to make sure that's working. I'm pretty sure the problem is with the wiring of the jdbc template or lack thereof. edit: let me provide a sample – nashpatty Nov 13 '17 at 18:55
  • I can help you if you show me your query, you can replace the sensitive information with other values – Youcef LAIDANI Nov 13 '17 at 18:58
  • `private static final String someCase = new StringBuilder().append("(case").append(" when count(*) >= 1 then 1").append(" else 0").append(" end) as some_case ").toString(); private static final String someQy = new StringBuilder().append("select ").append(someCase).append("from table_1 table1 ").append("where table1.table_1_id = ").append(" (select t1.table_1_id ").append(" from table_1 t1 ").append(" where t1.column1 = ? ").append(" and t1.column2 = ?) ").toString();` hope this helps – nashpatty Nov 13 '17 at 19:01

1 Answers1

2

You can't autowire easily for static method (little hack).

The easiest way to test something is to... write a test ;)

@RunWith(SpringJUnit4ClassRunner.class)
public class RepositoryImplTest {

    @Autowired
    private Repository repository;

    @Test
    public void testJob() throws Exception {
        //when
        boolean val = repository.runQuery();

        Assert.assertTrue(val);
    }
}

Check out Unit Testing section.

hya
  • 1,708
  • 2
  • 15
  • 22
  • @malhanniazi if this help you, you have to accept the answer read [How does accepting an answer work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Youcef LAIDANI Nov 14 '17 at 10:29