0

I'm using spring mvc. I want to test my service with Junit. I want to test the inclusion of a new user I write the class test:

@ContextConfiguration("/dispatcher-servlet.xml")
public class ServizioUtenteTest {

    @Autowired
    private ServizioUtente servizioutente;

    @Test
    public void testAggiungiUtente() {
        //fail("Not yet implemented");
        Utente utente=new Docente();

        utente.setCognome("Professore");
        utente.setPassword("tetxts");
        utente.setUsername("xxxx");

        servizioutente.aggiungiUtente(utente);


    }

    @Test

}

When I run the test I get:

java.lang.NullPointerException
    at org.rol.test.ServizioUtenteTest.testAggiungiUtente(ServizioUtenteTest.java:61)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
....)

The error is thrown on the line, why? help me!?

1 Answers1

1

You are missing SpringJUnit4ClassRunner:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/dispatcher-servlet.xml")
public class ServizioUtenteTest {
  //...

See also

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674