2015-11-22 13 views
6

Mam niepełnosprawnego JTable, który zapewnia menu:Kliknięcie niepełnosprawnych komponentów Swing

import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 

import javax.swing.JFrame; 
import javax.swing.JMenuItem; 
import javax.swing.JPopupMenu; 
import javax.swing.JTable; 

public class DisabledTableFrame extends JFrame { 

    public DisabledTableFrame() { 

     setSize(200, 100); 
     setTitle(getClass().getCanonicalName()); 
     setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     JTable table = new JTable(); 
     add(table); 

     table.addMouseListener(new MouseAdapter() { 

      @Override 
      public void mousePressed(MouseEvent e) { 
       mouseReleased(e); 
      } 

      @Override 
      public void mouseReleased(MouseEvent e) { 
       new PopupMenu(); 
      } 

     }); 

     table.setEnabled(false); 
     setVisible(true); 
    } 

    public static void main(String[] args) { 
     new DisabledTableFrame(); 

    } 

    private class PopupMenu extends JPopupMenu { 

     public PopupMenu() { 

      JMenuItem menuItem = new JMenuItem("TEST"); 
      add(menuItem); 
      setVisible(true); 
     } 
    } 
} 

więc podczas testowania tej funkcji z AssertJ Swing za pomocą:

import org.assertj.swing.edt.GuiActionRunner; 
import org.assertj.swing.edt.GuiQuery; 
import org.assertj.swing.fixture.FrameFixture; 
import org.assertj.swing.junit.testcase.AssertJSwingJUnitTestCase; 
import org.junit.Test; 

public class PopupTestCase extends AssertJSwingJUnitTestCase { 

    protected FrameFixture window; 

    @Override 
    protected void onSetUp() { 
     DisabledTableFrame mainFrame = GuiActionRunner 
       .execute(new GuiQuery<DisabledTableFrame>() { 
        protected DisabledTableFrame executeInEDT() { 

         return new DisabledTableFrame(); 
        } 
       }); 

     window = new FrameFixture(robot(), mainFrame); 
    } 

    @Test 
    public void popupShouldBeOpened() { 

     window.table().showPopupMenu(); 
    } 
} 

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>AssertJSwingInactiveTest</groupId> 
    <artifactId>AssertJSwingInactiveTest</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 

    <dependencies> 
     <dependency> 
      <groupId>org.assertj</groupId> 
      <artifactId>assertj-swing-junit-4.5</artifactId> 
      <version>1.2.0</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <sourceDirectory>src</sourceDirectory> 
     <plugins> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.3</version> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

Działa poprawnie, gdy tabela jest włączona. Ale() Badanie popupShoudBeOpened na stole niepełnosprawnych rzuca następujący wyjątek:

java.lang.IllegalStateException: Expecting component javax.swing.JTable[name=null, rowCount=0, columnCount=0, enabled=false, visible=true, showing=true] to be enabled 
at org.assertj.swing.driver.ComponentPreconditions.checkEnabled(ComponentPreconditions.java:68) 
at org.assertj.swing.driver.ComponentPreconditions.checkEnabledAndShowing(ComponentPreconditions.java:48) 
at org.assertj.swing.driver.ComponentDriver$2.executeInEDT(ComponentDriver.java:555) 
at org.assertj.swing.edt.GuiTask.run(GuiTask.java:38) 
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312) 
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:745) 
at java.awt.EventQueue.access$300(EventQueue.java:103) 
at java.awt.EventQueue$3.run(EventQueue.java:706) 
at java.awt.EventQueue$3.run(EventQueue.java:704) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
at java.awt.EventQueue.dispatchEvent(EventQueue.java:715) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) 
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) 
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) 
at org.assertj.swing.edt.GuiActionRunner.execute(GuiActionRunner.java:103) 
at org.assertj.swing.driver.ComponentDriver.checkInEdtEnabledAndShowing(ComponentDriver.java:552) 
at org.assertj.swing.driver.ComponentDriver.invokePopupMenu(ComponentDriver.java:519) 
at org.assertj.swing.fixture.AbstractJPopupMenuInvokerFixture.showPopupMenu(AbstractJPopupMenuInvokerFixture.java:95) 
at GUITestCase.popupShouldBeOpened(GUITestCase.java:27) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41) 
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:220) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 

jak menu podręczne oczywiście działa nawet na stole niepełnosprawnych, jak mogę dostać AssertJ do „right click” tabela wyłączone?

+0

Możesz opublikować kod proszę? – bircastri

+0

To assertj, który zgłasza wyjątek, ponieważ oczekuje włączenia tabeli. Podaj pełny opis wyjątku. –

+0

@bircastri Przezwyciężyłem problem do podstaw i dostarczyłem cały stacktrace. Wspaniale, że wciąż są ludzie, którzy używają AssertJ Swing. –

Odpowiedz

1

AsserJ oczekiwał, że testowany komponent zostanie włączony.
Po wyłączeniu zostanie wyświetlony wyjątek IllegalStateException. Przetestuj AbstractComponentFixture API here.

Jednym ze sposobów rozwiązania tego problemu jest podział na 2 testach i zawierają wyjątek, gdy oczekiwany jest stół jest wyłączony:

@Test 
public void popupShouldBeOpenedIfTableIsDisabled() { 
    try { 
     window.table().showPopupMenu(); 
    } catch (IllegalStateException e) { 
     // Continue normally if IllegalStateException was thrown since the table is disabled on purpose. 
    } 
} 

@Test 
public void popupShouldBeOpenedIfTableIsEnabled() { 
    window.table().showPopupMenu(); 
    // IllegalStateException will fail the test since the table is enabled. 
} 
+0

Niż za zajęcie się moim problemem. Problem polega na tym, że menu podręczne i tak nie zostanie otwarte. Ale muszę go otworzyć, aby kliknąć element menu. Czy istnieje sposób, aby wyłączyć sprawdzanie warunków wstępnych w AssertJ? –

+0

Nie ma za co. Problem polega na tym, że komponent sterownika zawsze sprawdza, czy włączono + pokazywanie EDT +. Jeśli nie zmienisz kodu źródłowego, te 3 warunki wstępne muszą zostać spełnione przed testem. –

Powiązane problemy