8

I'm getting error java.lang.NoClassDefFoundError when running some tests only some times.

Here's the set up: Tests are written in Scala with services in Scala and Java. Using ant and ivy as well.

Order.scala looks something like this:

  object Order extends JdbcEnabled {

  val orderServiceClientIpAddress = Network.localIpAddress
  val PersonalOffersSaleId = "123"
  lazy val checkoutClient = new CheckoutClientImpl(YamlConfigFactory.loadConfig(
    this.getClass.getClassLoader.getResourceAsStream("core_config.yaml")
  ).getRequiredSubConfig("core").getRequiredSubConfig(Environment.HostEnv))


  val storeList = new JLinkedList[Store]()
  storeList.add(OrderHelper.getSelectedStore)
  var skuList = OrderHelper.getAvailableSkus
  val skusForInternationalOrders = skuList


  def createOrder(){...}}

There are many tests running with TestNG. Sometimes all the tests pass without any problem, but sometimes they fail with this exception.

Here's a snippet of how a test calls Order api when it fails.

val orderNumber = Order.createOrder()

This is the entire stack trace when the test fails:

java.lang.NoClassDefFoundError: Could not initialize class com.api.Order$
    at com.CreateOrder.setUpOnce(CreateOrder.scala:35)

Line 35 in that class, CreateOrder.scala is:

val orderNumber = Order.createOrder()
Joseph Blue
  • 81
  • 1
  • 1
  • 3
  • I think it means you've got an inner class in Order that's failing during initialization. But this is presumably a Scalaism. – Hot Licks Oct 20 '12 at 12:24

4 Answers4

6

Generally, this kind of issue happens when there is an uncaught exception inside your singleton object. Try putting a try catch clause inside to get the origin of your error.

Ismail H
  • 4,226
  • 2
  • 38
  • 61
4

That is probably not your whole classpath, you must have the name of the missing class somewhere (probably after 'caused by..'). Maybe TestNG can be configured to show you complete stack traces.

Anyway, the initializer error means that 'object Order' threw an exception in its constructor, so look at what you are using there: maybe JDBC classes are missing? Or the configuration file you are retrieving via the class loader?

Iulian Dragos
  • 5,692
  • 23
  • 31
  • 1
    I had a similar problem, but the actual exception was not printed. I had to put "println" every other line to find out where it went wrong! – Mahdi Dec 10 '15 at 10:21
1

I think the problem is this:

  lazy val checkoutClient = new CheckoutClientImpl(YamlConfigFactory.loadConfig(
    this.getClass.getClassLoader.getResourceAsStream("core_config.yaml")
  ).getRequiredSubConfig("core").getRequiredSubConfig(Environment.HostEnv))

As they say here and here, seems to be a problem of getClassLoader or the classpath.

Community
  • 1
  • 1
Josemy
  • 810
  • 1
  • 12
  • 29
0

Your Order class seems sime kind of proxy one. It is not initialized.

user
  • 3,058
  • 23
  • 45