2

I'm using the below web app stack and when I run my TestNG test and the container gets initialized my test class fails to autowire the ExampleRepository with the below exception. Any ideas on how to map such a relationship? When I start a jetty web server with the same web application I don't get any startup exceptions, so it could be an issue with AbstractTransactionalTestNGSpringContextTests.

Stack:

    Spring - 3.2.2.RELEASE
    Spring Data - 1.3.0.RELEASE
    Hibernate - 3.6.10.Final
    TestNG - 6.1.1

Exception:

Cannot instantiate abstract class or interface: com.test.Example; nested exception is org.hibernate.InstantiationException

JPA Entities:

@Entity
@Table(name = "EXAMPLE")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
public abstract class Example {}


@Entity
@DiscriminatorValue("examplea")
public class ExampleA extends Example {}

@Entity
@DiscriminatorValue("exampleb")
public class ExampleB extends Example {}

Repository:

public interface ExampleRepository extends JpaRepository<Example, Long> {}

TestNG Class:

public class ExampleTest extends AbstractTransactionalTestNGSpringContextTests{
      @Autowired
      private ExampleRepository exampleRepository;
}
1
  • I would use specific repos ExempleARepository and ExempleBRepository Commented Dec 10, 2013 at 6:18

1 Answer 1

1

Make Example class as Interface and let other classes implement it. As exception clearly says you cannot instantiate an abstract class. Making Example class as interface you can instantiate its types i.e the classes implementing it.

Sign up to request clarification or add additional context in comments.

3 Comments

Example is a abstract class because it also has implementation code that is used by the class ExampleA and ExampleB
Thats fine but you wont be able to autowire its types. You can make your classes to implement another interface and extend this class as well and then try to autowire the interface types
ok, I think I understand what you're saying. Make Example a concrete non-abstract class and just create a Interface that ExampleA and ExampleB can implement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.