小编mas*_*asT的帖子

Mockito - 创建嵌套的模拟对象

我正在学习Mockito.我在为嵌套对象创建模拟时遇到问题.看到

public interface BaseManager {
    public Query createQuery(String queryString);
}
Run Code Online (Sandbox Code Playgroud)

和一个实现类

public class BaseManagerImpl implements BaseManager {
    @Autowired
    private SessionFactory sessionFactory;
    // ...
}
Run Code Online (Sandbox Code Playgroud)

模块级别的hibernate管理器,例如:

public interface RegistrationManager {
    @Transactional
    public List<Country> getCountries();
}
Run Code Online (Sandbox Code Playgroud)

和一个实现类

public class RegistrationManagerImpl implements RegistrationManager {
    @Autowired
    private BaseManager baseManager;
    // ...
}
Run Code Online (Sandbox Code Playgroud)

现在我在创建模拟基础管理器方面遇到了问题.我的测试类是:

 public class MockitoTest {
    private RegistrationManager registrationManager = new RegistrationManagerImpl();

    @Mock private BaseManager baseManager;

    @Mock private SessionFactory sessionFactory;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
    //  baseManager.setSessionFactory(sessionFactory);
        registrationManager.setBaseManager(baseManager);
    }
    // ...
    // …
Run Code Online (Sandbox Code Playgroud)

java mockito

7
推荐指数
2
解决办法
2万
查看次数

SockJS - '/ info'在连接服务器时导致Http 404

我主要按照以下帖子使用SockJS实现推送通知,

推送Java Webapp,SockJS客户端,SockJS Java服务器的通知.

我的sockJS客户端是:

var sock = new SockJS("http://localhost:8080/pusher");

sock.onmessage = function(event) {
    console.log("message: " + event.data);
    alert('received message echoed from server: ' + event.data);
};
Run Code Online (Sandbox Code Playgroud)

*服务器正在侦听相同的端口8080.但在运行时我得到错误404:

GET http://localhost:8080/pusher/info 404 (Not Found)
Run Code Online (Sandbox Code Playgroud)

StackOverflow上的这篇文章并没有解决我的问题.请检查我错过了什么.我是否需要注册客户端或发布服务器以启用推送通知.提前致谢!

java push-notification sockjs

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

C++:Union Destructor

union是用户定义的数据或类类型,在任何给定时间,它只包含其成员列表中的一个对象.假设需要动态分配所有可能的候选成员.对于Eg.

// Union Destructor
#include <string>
using namespace std;

union Person
{
private:
    char* szName;
    char* szJobTitle;
public:
    Person() : szName (nullptr), szJobTitle (nullptr) {}
    Person (const string& strName, const string& strJob)
    {
        szName = new char[strName.size()];
        strcpy (szName, strName.c_str());

        szJobTitle = new char [strJob.size()];
        strcpy (szJobTitle, strJob.c_str());    // obvious, both fields points at same location i.e. szJobTitle
    }
    ~Person()   // Visual Studio 2010 shows that both szName and szJobTitle
    {           // points to same location.
        if (szName) {
            delete[] …
Run Code Online (Sandbox Code Playgroud)

c++ destructor unions

5
推荐指数
0
解决办法
3897
查看次数

org.hibernate.PropertyNotFoundException:无法找到0的setter

我只使用HQL从表中获取选定的属性,维护非实体类对象的列表.对于Eg.我的实体类:

@Entity
@Table(name="STUDENT")
public class Student {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    @Column(name="NAME", columnDefinition="TEXT", length="60", nullable = false)
    private String name;

    @ManyToOne
    @JoinColumn(name = "Dept_id", nullable = false)
    private Department department;

    // Other fields...
    // Getter-Setters
}
Run Code Online (Sandbox Code Playgroud)

非持久性DTO类只有较少的类成员(比如名字):

public class StudentDTO {
    private String name;
    // Getter-Setter for name
}
Run Code Online (Sandbox Code Playgroud)

现在用

public List<StudentDTO> getStudents(Long deptId) {
    List<StudentDTO> students;

    Query query = session.createQuery("select student.name " +
            "from Student as student " +
            "where Dept_id =?").setResultTransformer(new AliasToBeanResultTransformer(StudentDTO.class));
    query.setString(0, Long.toString(deptId));

    students = CommonUtil.castList(StudentDTO.class, …
Run Code Online (Sandbox Code Playgroud)

java hibernate hql

4
推荐指数
2
解决办法
3万
查看次数

shared_ptr不报告引用的对象删除

我在MS Visual Studio 10中运行此代码,

#include <iostream>
#include <memory>
using namespace std;

class A
{
    int i;
public:
    A(int j) : i(j) {}
    ~A() {}
    void fun()
    {
        cout << "A::i = " << i << endl;
    }
};
int _tmain(int argc, _TCHAR* argv[])
{
    A aObj(12);
    std::shared_ptr<A> const pObj (&aObj,
                [] (A* pA) {
                    cout << "lambda deleter" << endl;
                });
    aObj.~A();
    pObj->fun();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这将打印/保存已删除的对象的数据成员,而不报告任何类型的错误.

请写信:

  1. 为什么shared_ptrpObj不报告(在运行时)底层对象已被删除?
  2. 由于我正在创建一个const shared_ptr,意味着不能使用它来引用任何其他对象,为什么在删除对象时不调用lambda.
  3. weak_ptr在类似情况下可能会有帮助.weak_ptr与语义一起使用,对对象的引用的生命周期超过它引用的对象.

c++ smart-pointers shared-ptr c++11

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