为什么junit测试在eclipse中失败但在ant中通过?

发布于 2024-07-14 16:23:47 字数 3004 浏览 3 评论 0原文

我有一个 JUnit 测试,用于检查以确保自定义 xml 序列化正常工作。 定制的 xml 序列化只是 Xstream 的一些定制转换器。 反序列化器可以工作,但由于某些原因,在 Eclipse 3.x 中,JUnit 无法序列化。 在 Ant 的命令行中,它工作得很好。 如果我调试并单步执行测试用例,它在 Eclipse 中也可以正常工作,但如果在失败的测试用例执行后放置一个断点,它仍然会失败。

是什么赋予了? 我有类路径问题吗?

一些附加信息:

预期:

<site>
  <name>origin</name>
  <version>0.6.0</version>
  <description>Stuff</description>
  <source>./fake-file.xml</source>
  <location>
    <latitude deg="44" min="26" sec="37.640"/>
    <longitude deg="-57" min="-38" sec="-6.877"/>
    <ellipsoid-height value="-79.256" units="meters"/>
    <geoid-height value="0.000" units="meters"/>
  </location>
</site>

实际:

<site>
  <name>origin</name>
  <version>0.6.0</version>
  <description>Stuff</description>
  <source>./fake-file.xml</source>
  <location>
    <latitude deg="44" min="26" sec="37.640"/>
    <longitude deg="-57" min="-38" sec="-6.877"/>
    <ellipsoid-height value="-79.256" units="meters"/>
    <geoid-height value="-79.256" units="meters"/>
  </location>
</site>

写入位置字段的代码:

public void marshal(Object source, 
                    HierarchicalStreamWriter writer,
                    MarshallingContext context)
{
   ILatLonEllipsoidHeightPoint aLoc = (ILatLonEllipsoidHeightPoint) source;
   synchronized(aLoc)
   {
      writer.startNode(LATITUDE);
      writer.addAttribute(DEGREES,
         Integer.toString(
           PointUnitConversions.getLatitudeHours(aLoc.getLatitude())));
      writer.addAttribute(MINUTES,
         Integer.toString(
           PointUnitConversions.getLatitudeMinutes(aLoc.getLatitude())));
      writer.addAttribute(SECONDS,
         String.format("%.3f", 
           PointUnitConversions.getLatitudeSeconds(aLoc.getLatitude())));
      writer.endNode();

      writer.startNode(LONGITUDE);
      writer.addAttribute(DEGREES, 
         Integer.toString(
           PointUnitConversions.getLongitudeHours(aLoc.getLongitude())));
      writer.addAttribute(MINUTES,
         Integer.toString(
           PointUnitConversions.getLongitudeMinutes(aLoc.getLongitude())));
      writer.addAttribute(SECONDS,
         String.format("%.3f", 
           PointUnitConversions.getLongitudeSeconds(aLoc.getLongitude())));      
      writer.endNode();

      writer.startNode(ELLIPSOID_HEIGHT);
      writer.addAttribute(VALUE, 
         String.format("%.3f", aLoc.getEllipsoidHeight()));
      writer.addAttribute(UNITS, METERS);
      writer.endNode();      

      writer.startNode(GEOID_HEIGHT);
      writer.addAttribute(VALUE, 
         String.format("%.3f", aLoc.getGeoidHeight()));
      writer.addAttribute(UNITS, METERS);
      writer.endNode();
   }
}

PointUnitConversions 调用执行明显的数学运算以获取十进制度并转换为组成部分的相应整数或双精度值。

正是位置的最后一个属性导致了失败。

I have a JUnit test that is checking to make sure that a customized xml serialization is working properly. The customized xml serialization is just a few custom converters for Xstream. The deserializers work, but for some reason in Eclipse 3.x, JUnit fails the serialization. In Ant on the command line, it works just fine. It also works just fine in Eclipse if I debug and step through the test case, but if put a break point after the failing testcase executes it fails still.

What gives? Am I having a class path issue?

Some additional information:

Expected:

<site>
  <name>origin</name>
  <version>0.6.0</version>
  <description>Stuff</description>
  <source>./fake-file.xml</source>
  <location>
    <latitude deg="44" min="26" sec="37.640"/>
    <longitude deg="-57" min="-38" sec="-6.877"/>
    <ellipsoid-height value="-79.256" units="meters"/>
    <geoid-height value="0.000" units="meters"/>
  </location>
</site>

Actual:

<site>
  <name>origin</name>
  <version>0.6.0</version>
  <description>Stuff</description>
  <source>./fake-file.xml</source>
  <location>
    <latitude deg="44" min="26" sec="37.640"/>
    <longitude deg="-57" min="-38" sec="-6.877"/>
    <ellipsoid-height value="-79.256" units="meters"/>
    <geoid-height value="-79.256" units="meters"/>
  </location>
</site>

The code that writes the location fields:

public void marshal(Object source, 
                    HierarchicalStreamWriter writer,
                    MarshallingContext context)
{
   ILatLonEllipsoidHeightPoint aLoc = (ILatLonEllipsoidHeightPoint) source;
   synchronized(aLoc)
   {
      writer.startNode(LATITUDE);
      writer.addAttribute(DEGREES,
         Integer.toString(
           PointUnitConversions.getLatitudeHours(aLoc.getLatitude())));
      writer.addAttribute(MINUTES,
         Integer.toString(
           PointUnitConversions.getLatitudeMinutes(aLoc.getLatitude())));
      writer.addAttribute(SECONDS,
         String.format("%.3f", 
           PointUnitConversions.getLatitudeSeconds(aLoc.getLatitude())));
      writer.endNode();

      writer.startNode(LONGITUDE);
      writer.addAttribute(DEGREES, 
         Integer.toString(
           PointUnitConversions.getLongitudeHours(aLoc.getLongitude())));
      writer.addAttribute(MINUTES,
         Integer.toString(
           PointUnitConversions.getLongitudeMinutes(aLoc.getLongitude())));
      writer.addAttribute(SECONDS,
         String.format("%.3f", 
           PointUnitConversions.getLongitudeSeconds(aLoc.getLongitude())));      
      writer.endNode();

      writer.startNode(ELLIPSOID_HEIGHT);
      writer.addAttribute(VALUE, 
         String.format("%.3f", aLoc.getEllipsoidHeight()));
      writer.addAttribute(UNITS, METERS);
      writer.endNode();      

      writer.startNode(GEOID_HEIGHT);
      writer.addAttribute(VALUE, 
         String.format("%.3f", aLoc.getGeoidHeight()));
      writer.addAttribute(UNITS, METERS);
      writer.endNode();
   }
}

The PointUnitConversions calls do the obvious math to take a decimal degrees and convert to corresponding integer or double values for the component parts.

Its just that last attribute of location that is causing the failure.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执手闯天涯 2024-07-21 16:23:47

调试一下就可以正常工作了吗? 听起来像是一个时间问题; 调试它会减慢它的运行速度。 你能发布一些代码吗?

编辑:

感谢您添加信息。 看来,如果您单步执行它时它没有失败,但如果您正常运行它就会失败,我敢打赌它是计时/线程问题。 您是否在某处的线程中执行此操作? 有什么争议吗? 还是竞争条件?

It works fine if you debug it? Sounds like a timing issue; and debugging it slows it down enough to work. Can you post some code?

Edit:

Thanks for adding the info. It seems that if its NOT failing when you step through it, but it fails if you just run it normally, I would bet its a timing/Threading issues. Are you doing this in a Thread somewhere? Is there some contention? Or a race condition?

清欢 2024-07-21 16:23:47

有些东西不一样了。

根据您发布的内容无法判断,但我的猜测可能是:

  1. Ant、命令 shell 和 Eclipse 使用不同的 JVM,
  2. Ant 和 Eclipse 的 CLASSPATH 中的 JAR 不同,
  3. XML 编码不同。

Something's different.

Can't tell based on what you posted, but my guesses might be:

  1. Different JVM used by Ant, command shell, and Eclipse,
  2. Different JARs in the CLASSPATH for Ant and Eclipse,
  3. Different XML encodings.
最近可好 2024-07-21 16:23:47

可能使用的 XML 库不同。 例如,使用以下小代码片段找出使用的内容:

import org.w3c.dom.Text;
public class TextTest {
    public static void main(String[] args) {
        System.out.println(Text.class.getResource("Text.class"));
    }
}

Probably not the same XML lib used. Find out which is used using for example this small code piece:

import org.w3c.dom.Text;
public class TextTest {
    public static void main(String[] args) {
        System.out.println(Text.class.getResource("Text.class"));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文