小编Mar*_*iam的帖子

如何定义构建器模式层次结构,可以按任何顺序调用setter

考虑带有抽象Builder的抽象Data类:

abstract class Data {

    abstract static class Builder<T extends Data> {

        private String one;

        protected Builder() {
            this.one = null;
        }

        public final Builder<T> withOne(final String value) {
            this.one = value;
            return this;
        }

        protected abstract T build();
    }

    private final String one;

    protected Data(final Builder<? extends Data> builder) {
        this.one = builder.one;
    }

    public final String getOne() {
        return this.one;
    }
}
Run Code Online (Sandbox Code Playgroud)

该类已扩展,其中还包括自己的扩展构建器:

public final class Extension extends Data {

    public static final class ExtensionBuilder extends Data.Builder<Extension> { …
Run Code Online (Sandbox Code Playgroud)

java generics abstract-class builder

7
推荐指数
1
解决办法
179
查看次数

如何检索参数化类的类

请考虑以下代码:

public class Context {
    private final Class<?> clazz;
    private final String resource;
    private final com.thirdparty.Context context;

    public Context(final String resource, final Class<?> clazz) {
        this.clazz = clazz;
        this.resource = resource;
        this.context = com.thirdparty.Context.newInstance(this.clazz);
    }

    public String marshall(final Object element) {
        return this.context.marshall(element);
    }

    public Object unmarshall(final String element) {
        return this.context.unmarshall(element);
    }
}
Context context = new Context("request.xsd", Request.class);

// Marshall
Request request = new Request();
String xml = context.marshall(request);

// Unmarshall
Request roundTrip = Request.cast(context.unmarshall(xml));
Run Code Online (Sandbox Code Playgroud)

我试图用泛类版本的Context类替换它:

public class …
Run Code Online (Sandbox Code Playgroud)

java generics

5
推荐指数
1
解决办法
76
查看次数

使用Win API确定可执行文件的实例是否已在运行

我需要确保只运行一个我的C++应用程序实例.

使用Win API我该怎么做;

  1. 检索有关我当前应用程序的信息? GetCurrentProcess()会给我一个处理我的应用程序,如何检索有关它的信息

  2. 检索用户的所有正在运行的进程的列表? EnumProcesses()给出一个列表,但似乎需要预先分配的缓冲区,那么如何找出当前正在运行的进程数?

  3. 我需要将我的服务器的exe名称与正在运行的进程进行比较,如果找到多个,则会引发错误

注意:我不能使用任何boost库,我对使用a感兴趣mutex,在类似的帖子中看到.

c++ winapi

3
推荐指数
1
解决办法
1469
查看次数

在同一个类中调用另一个构造函数

我有一个有2个公共构造函数的类,我想调用一个私有构造函数:

class CDeviceTSGetObservationResponse : public CDeviceServerResponse
{
public:
   /**
    * Public constructor. Used to construct a response containing
    * the required information from the TotalStation device.
    *
    * @param horizontalAngle
    *           The horizontal angle.
    * @param verticalAngle
    *           The vertical angle.
    * @param slopeDistance
    *           The slope distance.
    */
   CDeviceTSGetObservationResponse(double horizontalAngle, 
                                   double verticalAngle, 
                                   double slopeDistance)
       : CDeviceTSGetObservationResponse(CDeviceServerResponse::SUCCESS_MESSAGE,
                                         horizontalAngle,
                                         verticalAngle,
                                         slopeDistance) {}

   /**
    * Public constructor. Used to construct a response containing
    * the error message from the TotalStation device.
    * …
Run Code Online (Sandbox Code Playgroud)

c++ constructor

2
推荐指数
1
解决办法
279
查看次数

使用Guice,如何将单元测试中的模拟对象注入到要测试的类中

考虑以下代码:

@Singleton
public class MyServiceImpl {
    public int doSomething() {
        return 5;
    }
}

@ImplementedBy(MyServiceImpl.class)
public interface MyService {
    public int doSomething();
}

public class MyCommand {
    @Inject private MyService service;

    public boolean executeSomething() {
        return service.doSomething() > 0;
    }
}

public class MyCommandTest {
    @InjectMocks MyServiceImpl serviceMock;
    private MyCommand command;

    @Before public void beforeEach() {
        MockitoAnnotations.initMocks(this);
        command = new MyCommand();
        when(serviceMock.doSomething()).thenReturn(-1); // <- Error here
    }

    @Test public void mockInjected() {
        boolean result = command.executeSomething();
        verify(serviceMock).doSomething();
        assertThat(result, equalTo(false));
    } …
Run Code Online (Sandbox Code Playgroud)

java dependency-injection guice mockito

1
推荐指数
1
解决办法
6737
查看次数

测试异常时如何验证方法被调用

考虑以下代码:

@Rule ExpectedException expected = ExpectedException.none();
@Mock private MyObject mockMyObject;
private Converter converter; // Object under test

@Before public void before() {
    MockitoAnnotations.initMocks(this);
    when(mockMyObject.doSomething1()).thenReturn(1);
    when(mockMyObject.doSomething2()).thenReturn("2");
}

@After public void after() {
    verifyNoMoreInteractions(mockMyObject); // Exception test fails here
}

@Test public void testConverter() {
    assertThat(converter.convert(mockMyObject), notNullValue());
    verify(mockMyObject).doSomething1();
    verify(mockMyObject).doSomething2();
}

@Test public void testConverterException() {
    when(mockMyObject.doSomething1()).thenThrow(MyException.class);
    expected.expect(MyException.class);
    converter.convert(mockMyObject);
    verify(mockMyObject).doSomething1(); // Never gets called
}
Run Code Online (Sandbox Code Playgroud)

我想要做的是,在异常测试中,标记我期望 doSomething1() 将被调用。然而,在 converter.convert() 处抛出异常,这意味着永远不会调用 verify() 调用。因此 verifyNoMoreInteractions() 在 after() 中失败。

注意:这是一个非常通用的示例,用于隐藏我们的任何内部代码。

java verify mockito

1
推荐指数
1
解决办法
3028
查看次数