Feras Odeh
Feras Odeh

Reputation: 9306

Grails: NPE in Unit Test

I'm writing a unit test for my domain class but I get a NPE when I tried to run the following test:

def st

void setUp(){
    super.setUp()
    mockForConstraintsTests(Student)        
    st=new Student(firstName:"Feras",lastName:"Ahmad")


}

void testMinSize() {        
    st.firstName="J"
    assertFalse st.validate();              
}

I got the NPE on the first line of testMinSize method. what is wrong with that? I think setUp method is called before every test. Is this true?

Thanks,

Upvotes: 1

Views: 128

Answers (2)

RRK
RRK

Reputation: 465

Add @Mock(Student)

at the top of your Test class

Upvotes: 1

Nicolas Zozol
Nicolas Zozol

Reputation: 7048

Maybe you could try the @Before annotation

@Before
void setUp(){
    super.setUp()
    print "I'm there"
    mockForConstraintsTests(Student)        
    st=new Student(firstName:"Feras",lastName:"Ahmad")
}

The documentation puts sometime the annotation, sometime not. Maybe it depends on the grails/JUnit version.

Upvotes: 1

Related Questions